objc

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package objc provides cached Objective-C runtime helpers.

This package wraps purego/objc to provide selector caching for better performance.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnrecognizedSelector = &UnrecognizedSelectorError{}

ErrUnrecognizedSelector is returned when an object does not respond to a selector.

Functions

func AddMethod

func AddMethod(cls Class, sel SEL, impl any, types string) bool

AddMethod adds a new method to a class.

func CallWithError

func CallWithError(id ID, sel SEL, args ...any) error

CallWithError calls a void-returning selector and handles the NSError** pattern.

func ConvertSlice

func ConvertSlice[T any](ids []ID, convert func(ID) T) []T

ConvertSlice maps []ID to []T using a converter function.

func ConvertSliceToStrings

func ConvertSliceToStrings(ids []ID) []string

ConvertSliceToStrings maps []ID to []string via IDToString.

func GoString

func GoString(cstr *byte) string

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 IDToString

func IDToString(id ID) string

IDToString converts an NSString ID to a Go string.

func MustSend

func MustSend[T any](id ID, sel SEL, args ...any) T

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

func NewActionTarget(owner ID, fn func(sender ID)) (target ID, sel SEL)

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

func RespondsToSelector(id ID, sel SEL) bool

RespondsToSelector checks if an object responds to the given selector. This is the safe way to check before calling a method.

func SafeSend

func SafeSend[T any](id ID, sel SEL, args ...any) (T, error)

SafeSend calls a selector only if the object responds to it. Returns the zero value and ErrUnrecognizedSelector if the selector is not recognized. This prevents NSInvalidArgumentException crashes from unrecognized selectors.

func Send

func Send[T any](id ID, sel SEL, args ...any) T

Send calls purego.Send with the given arguments. If any argument implements IDGetter (has GetID() method), the ID is automatically extracted. This allows passing struct wrappers like VZVirtualMachine directly instead of having to call .ID or .GetID() manually. Nil interface values are converted to ID(0) to avoid purego panics. When T is []ID, the return value from the ObjC call (an NSArray) is automatically converted to a Go slice via NSArrayToSlice.

func SendWithError

func SendWithError[T any](id ID, sel SEL, args ...any) (T, error)

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.

Types

type Block

type Block = purego.Block

Type aliases for convenience

func NewBlock

func NewBlock(fn any) Block

NewBlock creates an Objective-C block from a Go function. The Go function must take a Block as its first argument. Use Block.Release() to free the block when it is no longer in use.

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.

func CArray

func CArray(v any) CArrayArg

CArray marks a slice argument for C-array pointer conversion in Send.

type Class

type Class = purego.Class

Type aliases for convenience

func GetClass

func GetClass(name string) Class

GetClass returns the class with the given name.

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 FieldDef

type FieldDef = purego.FieldDef

Type aliases for convenience

type ID

type ID = purego.ID

Type aliases for convenience

func IDFrom

func IDFrom(ptr unsafe.Pointer) ID

IDFrom converts a raw pointer to an ID.

func IDValueAt added in v0.3.6

func IDValueAt(addr uintptr) ID

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

func NSArrayToSlice(array ID) []ID

NSArrayToSlice converts an NSArray ID into a []ID by calling count and objectAtIndex:.

func String

func String(s string) ID

String converts a Go string to an NSString object. This must be called before passing Go strings to Objective-C methods that expect NSString*. The returned ID is autoreleased.

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 MethodDef

type MethodDef = purego.MethodDef

Type aliases for convenience

type Protocol

type Protocol = purego.Protocol

Type aliases for convenience

func GetProtocol

func GetProtocol(name string) *Protocol

GetProtocol returns the protocol with the given name, or nil if not found.

type SEL

type SEL = purego.SEL

Type aliases for convenience

func RegisterName

func RegisterName(name string) SEL

RegisterName registers a selector with the Objective-C runtime. This is the same as Sel but without caching - use Sel for repeated calls.

func Sel

func Sel(name string) SEL

Sel returns a cached selector for the given name. This avoids the global lock in purego.RegisterName on repeated calls.

type UnrecognizedSelectorError

type UnrecognizedSelectorError struct {
	Selector string
}

UnrecognizedSelectorError indicates an object does not respond to a selector.

func (*UnrecognizedSelectorError) Error

func (e *UnrecognizedSelectorError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL