Documentation
¶
Overview ¶
Package objc provides cached Objective-C runtime helpers.
This package wraps purego/objc to provide selector caching for better performance.
Index ¶
- Variables
- func AddMethod(cls Class, sel SEL, impl any, types string) bool
- func AssociateBlockWithReceiver(receiver ID, key *byte, block Block)
- func AutoreleasePool(fn func())
- func CallWithError(id ID, sel SEL, args ...any) error
- func ConvertSlice[T any](ids []ID, convert func(ID) T) []T
- func ConvertSliceToStrings(ids []ID) []string
- func GoString(cstr *byte) string
- func GoStringPtr(cstr *byte) *string
- func IDToString(id ID) string
- func IDToStringPtr(id ID) *string
- func MustSend[T any](id ID, sel SEL, args ...any) T
- func NewActionTarget(owner ID, fn func(sender ID)) (target ID, sel SEL)
- func RegisterClassPair(cls Class)
- func RespondsToSelector(id ID, sel SEL) bool
- func SafeSend[T any](id ID, sel SEL, args ...any) (T, error)
- func Send[T any](id ID, sel SEL, args ...any) T
- func SendWithError[T any](id ID, sel SEL, args ...any) (T, error)
- func SetBlockSignature(block Block, signature []byte) bool
- func SetNSErrorBlockSignature(block Block) bool
- type Block
- type CArrayArg
- type Class
- type FieldDef
- type ID
- type IDGetter
- type MethodDef
- type Protocol
- type SEL
- type UnrecognizedSelectorError
Constants ¶
This section is empty.
Variables ¶
var ErrUnrecognizedSelector = errors.New("unrecognized selector")
ErrUnrecognizedSelector is the sentinel error for an unavailable selector.
Functions ¶
func AssociateBlockWithReceiver ¶ added in v0.6.1
AssociateBlockWithReceiver ties the lifetime of block to receiver via objc_setAssociatedObject with OBJC_ASSOCIATION_RETAIN_NONATOMIC.
When the receiver deallocates, the Objective-C runtime releases the associated block, which invokes purego's dispose callback and clears the Go closure from the block cache.
Passing the same key on a subsequent call replaces the prior association: the runtime releases the previous block before retaining the new one. This is the mechanism used by setter-style escaping methods (set*Block:, set*Handler:, set*Callback:) to free the prior block when overwritten.
The function is unexported: keys are package-level vars emitted by applegen at each call site, so external callers have no need to construct them. The block must be a Go-owned block returned by NewBlock; the caller transfers ownership.
func AutoreleasePool ¶ added in v0.5.0
func AutoreleasePool(fn func())
AutoreleasePool executes fn within an Objective-C autorelease pool. Any autoreleased objects created during fn are released when fn returns.
func CallWithError ¶
CallWithError calls a void-returning selector and handles the NSError** pattern.
func ConvertSlice ¶
ConvertSlice maps []ID to []T using a converter function.
func ConvertSliceToStrings ¶
ConvertSliceToStrings maps []ID to []string via IDToString.
func GoString ¶
GoString converts a C string (*byte from UTF8String) to a Go string. This is needed because UTF8String returns const char*, not an ObjC object.
func GoStringPtr ¶ added in v0.5.3
GoStringPtr converts a nullable C string to a heap-backed *string.
func IDToStringPtr ¶ added in v0.5.3
IDToStringPtr converts a nullable NSString ID to a heap-backed *string.
func MustSend ¶
MustSend calls a selector and panics with a clear error if the object doesn't respond. Use this when you expect the selector to always exist but want a clearer panic message than the NSInvalidArgumentException.
func NewActionTarget ¶
NewActionTarget creates an Objective-C trampoline object that calls fn when it receives the "invoke:" selector. The trampoline is associated with owner via objc_setAssociatedObject so it is retained for the owner's lifetime and cleaned up automatically when the owner is deallocated or a new action target replaces it.
Returns the trampoline ID and the selector to wire as the action.
func RegisterClassPair ¶
func RegisterClassPair(cls Class)
RegisterClassPair registers a class pair with the runtime.
func RespondsToSelector ¶
RespondsToSelector checks if an object responds to the given selector. This is the safe way to check before calling a method.
func SafeSend ¶
SafeSend calls a selector only if the object responds to it. Returns the zero value and an error matching ErrUnrecognizedSelector if the selector is not recognized. This prevents NSInvalidArgumentException crashes from unrecognized selectors.
func Send ¶
Send calls objc_msgSend with the given arguments.
When all arguments are uintptr-sized primitives (ID, SEL, Class, uintptr, bool, or integer types) and T is ID or struct{}, Send uses a pre-registered typed function that avoids reflect.MakeFunc — zero allocations, ~10x faster.
Otherwise Send falls back to purego.Send[T] with full argument processing: IDGetter extraction, nil→ID(0), CArrayArg conversion, and NSArray→[]ID.
func SendWithError ¶
SendWithError calls a selector handles the NSError** pattern. It assumes the method accepts an NSError** as its last argument. It automatically appends the error pointer to the arguments.
func SetBlockSignature ¶ added in v0.6.10
SetBlockSignature sets the Objective-C runtime signature for block. The signature storage must outlive block.
func SetNSErrorBlockSignature ¶ added in v0.6.10
SetNSErrorBlockSignature sets block's signature to a single NSError argument.
Types ¶
type CArrayArg ¶
type CArrayArg struct {
// contains filtered or unexported fields
}
CArrayArg marks a Go slice argument that should be passed to Objective-C as a pointer to contiguous C array storage.
Use CArray at call sites for APIs that take `*T` and a paired `count` parameter.
type Class ¶
Type aliases for convenience
func RegisterClass ¶
func RegisterClass(name string, superClass Class, protocols []*Protocol, ivars []FieldDef, methods []MethodDef) (Class, error)
RegisterClass registers a new Objective-C class with the runtime. The class inherits from superClass and implements the given protocols. ivars defines instance variables, methods defines the class methods.
type ID ¶
Type aliases for convenience
func IDValueAt ¶ added in v0.3.6
IDValueAt loads an Objective-C object ID stored at a symbol address.
Dynamic libraries commonly export Objective-C object constants as pointers to storage containing the real object ID. Dlsym returns the storage address, so callers must load the pointer-sized value stored there.
func NSArrayToSlice ¶
NSArrayToSlice converts an NSArray ID into a []ID by calling count and objectAtIndex:.
type IDGetter ¶
type IDGetter interface {
GetID() ID
}
IDGetter is implemented by types that wrap an Objective-C object ID. This allows objc.Send to automatically extract the ID from wrapper types.
type Protocol ¶
Type aliases for convenience
func GetProtocol ¶
GetProtocol returns the protocol with the given name, or nil if not found.
type SEL ¶
Type aliases for convenience
func RegisterName ¶
RegisterName registers a selector with the Objective-C runtime. This is the same as Sel but without caching - use Sel for repeated calls.
type UnrecognizedSelectorError ¶
type UnrecognizedSelectorError struct {
Selector string
}
UnrecognizedSelectorError records a selector an object does not respond to.
func (*UnrecognizedSelectorError) Error ¶
func (e *UnrecognizedSelectorError) Error() string
func (*UnrecognizedSelectorError) Unwrap ¶ added in v0.6.0
func (e *UnrecognizedSelectorError) Unwrap() error
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package objcbridge contains small Objective-C runtime helpers.
|
Package objcbridge contains small Objective-C runtime helpers. |