obj

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Rendered for darwin/amd64

Overview

Package obj defines Object, a Go handle for an Objective-C object whose specific type is not known. Some Objective-C APIs hand back a plain object ("id") — for example an element read from an untyped collection — and the generated code represents those as an Object.

Every generated wrapper type (String, VirtualMachine, and so on) is also an Object, so a specific wrapper can be passed anywhere an Object is expected. Going the other way, IsKind reports an object's Objective-C class, which lets you safely convert an Object to the specific wrapper you expect.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As[T any](o Object, className string, fromID func(objc.ID) T) (T, bool)

As narrows an Object to a specific wrapper type, after checking the object's Objective-C class. It is the idiomatic way to recover a typed value from an "id" return — for example an element read from an untyped dictionary:

dict, ok := obj.As(value, "NSDictionary", foundation.DictionaryFromID)

fromID is the wrapper type's generated FromID constructor. ok is false (and the result the type's zero value) when o is nil or is not an instance of className.

func Bytes

func Bytes(o Object) []byte

Bytes copies the contents of an NSData object into a Go byte slice. It returns nil when o is nil. Use it to read the bytes of an NSData handed back as an Object (for example a value read from a dictionary).

func ID

func ID(o Object) objc.ID

ID returns the underlying Objective-C object pointer of o (0 when o is nil).

This is the deliberate escape hatch for advanced interoperation the generated API cannot express — sending a selector by hand, installing a custom delegate implemented as a runtime class, or any other direct Objective-C dispatch. It does NOT expose the raw bindings, so a package using it stays free of the bindings/frameworks dependency; prefer the generated methods wherever they suffice and reach for ID only for genuine custom Objective-C work.

Types

type Object

type Object interface {
	objref.Object
	// Description returns the object's human-readable description text (the
	// result of Objective-C's -description).
	Description() string
	// IsEqual reports whether this object is equal to another, as Objective-C
	// itself decides (its -isEqual: method).
	IsEqual(other Object) bool
	// IsKind reports whether the object is an instance of the named Objective-C
	// class, or of a subclass of it. Use it to check an object's type before
	// converting it to a specific wrapper.
	IsKind(className string) bool
	// Release relinquishes the Go side's reference to the object immediately,
	// instead of waiting for the garbage collector. Idempotent; after Release
	// the object's methods are no-ops returning zero values.
	Release()
}

Object is a Go handle for an Objective-C object.

Only types that embed an objref.Handle can satisfy Object (the embedded interface has an unexported method), and the Handle supplies Release — so every implementation, generated or hand-written, gets the same lifecycle behavior.

func Adopt

func Adopt(id objc.ID) Object

Adopt returns an Object for an Objective-C object the caller already owns a reference to — for example the result of a create function annotated OS_OBJECT_RETURNS_RETAINED, or any other +1 result. Unlike Wrap it does NOT retain the object; it only arranges the matching release once Go stops using it. Wrapping an already-retained result with Wrap would leak one reference.

func Wrap

func Wrap(id objc.ID) Object

Wrap returns an Object for the given Objective-C object pointer (nil for 0). It keeps the object alive for as long as the returned value is reachable and releases it afterward.

func WrapUnmanaged added in v0.18.0

func WrapUnmanaged(id objc.ID) Object

WrapUnmanaged returns an Object for an opaque handle that is NOT reference-counted — a plain C handle with its own lifetime and no CFTypeID (e.g. AudioComponent, AudioQueueRef), obtained from a Get/Find/Create function. It installs NEITHER a retain NOR a release finalizer: objc_retain / objc_release on such a pointer is undefined and crashes (the pointer is not an object). The caller owns the handle's lifetime via the type's own dispose function, exactly as with the raw unsafe.Pointer these replace. Calling Release on the returned Object is likewise unsafe and must be avoided for these handles.

Jump to

Keyboard shortcuts

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