objc

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: MIT, BSD-2-Clause Imports: 10 Imported by: 2

Documentation

Overview

Gain low-level access to the Objective-C runtime and the Objective-C root types.

If it's not documented it's not meant to be used directly, but you can find the equivalent type or function in the Apple documentation.

Apple Documentation

Index

Constants

View Source
const (
	PropertyAttributeNameNonatomic = "N"
	PropertyAttributeNameStrong    = "&"
	PropertyAttributeNameRetain    = PropertyAttributeNameStrong
	PropertyAttributeNameWeak      = "W"
	PropertyAttributeNameReadonly  = "R"
	PropertyAttributeNameGetter    = "G"
	PropertyAttributeNameSetter    = "S"
	PropertyAttributeNameIvar      = "V"
	PropertyAttributeNameType      = "T"
)
View Source
const (
	ASSOCIATION_ASSIGN           = 0     // Specifies a weak reference to the associated object.
	ASSOCIATION_RETAIN_NONATOMIC = 1     // Specifies a strong reference to the associated object. The association is not made atomically.
	ASSOCIATION_COPY_NONATOMIC   = 3     // Specifies that the associated object is copied. The association is not made atomically.
	ASSOCIATION_RETAIN           = 01401 // Specifies a strong reference to the associated object. The association is made atomically.
	ASSOCIATION_COPY             = 01403 //Specifies that the associated object is copied. The association is made atomically.
)

Variables

This section is empty.

Functions

func AddClassMethod

func AddClassMethod(class IClass, sel Selector, f any) bool

AddClassMethod adds a class method using a Go function. The first argument of the Go function should be the class, the second argument should be the method selector.

func AddMethod

func AddMethod(class IClass, sel Selector, f any) bool

AddMethod adds an instance method using a Go function. The first argument of the Go function should be the object instance, the second argument should be the method selector.

func AssociationKey

func AssociationKey(name string) unsafe.Pointer

AssociationKey return key for AssociatedObject

func Call

func Call[T any](o Handle, selector Selector, params ...any) T

Call sends a message to the Handle and returns a value

func CallBlock

func CallBlock[T any](b Block, params ...any) T

func DisposeClass

func DisposeClass(class IClass)

Destroys a class and its associated metaclass. Full Topic

func ForceCast

func ForceCast[S any, T any](v S) T

func Ptr

func Ptr(o Handle) unsafe.Pointer

Ptr returns unsafe.Pointer or nil

func RegisterClass

func RegisterClass(class IClass)

Registers a class that was allocated using AllocateClass Full Topic

func RemoveAssociatedObjects

func RemoveAssociatedObjects(o IObject)

Removes all associations for a given object. Full Topic

func ReplaceClassMethod

func ReplaceClassMethod(class IClass, sel Selector, f any)

ReplaceClassMethod replaces a class method using a Go function. The first argument of the Go function should be the class, the second argument should be the method selector.

func ReplaceMethod

func ReplaceMethod(class IClass, sel Selector, f any)

ReplaceMethod replaces an instance method using a Go function. The first argument of the Go function should be the object instance, the second argument should be the method selector.

func Retain

func Retain(o IObject)

Retain will retain the object and set a finalizer for the Go GC to release. This is the preferred way to retain objects. Note the object must be passed by reference.

func SetAssociatedObject

func SetAssociatedObject(o IObject, key unsafe.Pointer, value IObject, policy AssociationPolicy)

Sets an associated value for a given object using a given key and association policy. Full Topic

func SetDeallocListener

func SetDeallocListener(o Object, listener func())

SetDeallocListener set a listener to be invoked when object ref count is decreased to zero(so the object is dealloced). Call dealloc method directly, but ref count is still large than 0 will not trigger the listener. Call multi times will remove previouse listener.

func ToGoBytes

func ToGoBytes(p unsafe.Pointer) []byte

func ToGoMap

func ToGoMap(ptr unsafe.Pointer, mapType reflect.Type) reflect.Value

func ToGoSlice

func ToGoSlice(ptr unsafe.Pointer, sliceType reflect.Type) reflect.Value

func ToGoString

func ToGoString(p unsafe.Pointer) string

func ToNSArray

func ToNSArray(slice reflect.Value) unsafe.Pointer

func ToNSData

func ToNSData(bytes []byte) unsafe.Pointer

func ToNSDict

func ToNSDict(m reflect.Value) unsafe.Pointer

func ToNSString

func ToNSString(s string) unsafe.Pointer

func WithAutoreleasePool

func WithAutoreleasePool(task func())

WithAutoreleasePool runs code in a new AutoreleasePool.

Types

type AssociationPolicy

type AssociationPolicy uintptr

Type to specify the behavior of an association. Full Topic

type Block

type Block struct {
	// contains filtered or unexported fields
}

func BlockFrom

func BlockFrom(ptr unsafe.Pointer) Block

func CreateGlobalBlock

func CreateGlobalBlock(f any) Block

CreateMallocBlock wraps a Go function as a global Block.

func CreateMallocBlock

func CreateMallocBlock(f any) Block

CreateMallocBlock wraps a Go function as an autoreleased Block.

func (Block) Copy

func (b Block) Copy() Block

func (Block) Ptr

func (b Block) Ptr() unsafe.Pointer

func (Block) Release

func (b Block) Release()

type Category

type Category struct {
	// contains filtered or unexported fields
}

func (Category) Ptr

func (c Category) Ptr() unsafe.Pointer

type Class

type Class struct {
	// contains filtered or unexported fields
}

func AllocateClass

func AllocateClass(superClass Class, name string, extraBytes uint) Class

Creates a new class and metaclass. Full Topic

func GetClass

func GetClass(name string) Class

GetClass returns an Objective-C Class by name

func (Class) AddMethod

func (c Class) AddMethod(sel Selector, imp IMP, types string) bool

func (Class) AddProperty

func (c Class) AddProperty(name string, attributes []PropertyAttribute) bool

func (Class) AddProtocol

func (c Class) AddProtocol(protocol Protocol) bool

func (Class) ClassMethod

func (c Class) ClassMethod(sel Selector) Method

func (Class) CopyMethodList

func (c Class) CopyMethodList() []Method

func (Class) CopyPropertyList

func (c Class) CopyPropertyList() []Property

func (Class) CreateInstance

func (c Class) CreateInstance(idxIvars uint) Object

func (Class) InstanceMethod

func (c Class) InstanceMethod(sel Selector) Method

func (Class) MetaClass

func (c Class) MetaClass() Class

func (Class) MethodImplementation

func (c Class) MethodImplementation(sel Selector) IMP

func (Class) MethodImplementationStret

func (c Class) MethodImplementationStret(sel Selector) IMP

func (Class) Name

func (c Class) Name() string

func (Class) Property

func (c Class) Property(name string) Property

func (Class) Ptr

func (c Class) Ptr() unsafe.Pointer

func (Class) ReplaceMethod

func (c Class) ReplaceMethod(sel Selector, imp IMP, types string) IMP

func (Class) ReplaceProperty

func (c Class) ReplaceProperty(name string, attributes []PropertyAttribute)

func (Class) RespondsToSelector

func (c Class) RespondsToSelector(sel Selector) bool

func (Class) SetVersion

func (c Class) SetVersion(version int)

func (Class) SuperClass

func (c Class) SuperClass() Class

func (Class) Version

func (c Class) Version() int

type Handle

type Handle interface {
	// Ptr returns the underlying unsafe.Pointer
	Ptr() unsafe.Pointer
}

Handle is an interface for holding an Objective-C pointer

type IClass

type IClass interface {
	Handle
	CreateInstance(idxIvars uint) Object
	Name() string
	SetVersion(version int)
	Version() int
	MetaClass() Class
	SuperClass() Class
	RespondsToSelector(sel Selector) bool
	AddMethod(sel Selector, imp IMP, types string) bool
	ReplaceMethod(sel Selector, imp IMP, types string) IMP
	MethodImplementation(sel Selector) IMP
	MethodImplementationStret(sel Selector) IMP
	InstanceMethod(sel Selector) Method
	ClassMethod(sel Selector) Method
	Property(name string) Property
	AddProperty(name string, attributes []PropertyAttribute) bool
	ReplaceProperty(name string, attributes []PropertyAttribute)
	AddProtocol(protocol Protocol) bool
	CopyMethodList() []Method
	CopyPropertyList() []Property
}

type IMP

type IMP struct {
	// contains filtered or unexported fields
}

func IMPFrom

func IMPFrom(ptr unsafe.Pointer) IMP

func IMPWithBlock

func IMPWithBlock(b Block) IMP

func (IMP) Block

func (i IMP) Block() Block

func (IMP) Ptr

func (i IMP) Ptr() unsafe.Pointer

func (IMP) RemoveBlock

func (i IMP) RemoveBlock() bool

type IObject

type IObject interface {
	Handle
	IsNil() bool

	Class() Class
	IsKindOfClass(class Class) bool
	IsMemberOfClass(class Class) bool
	RespondsToSelector(sel Selector) bool
	ConformsToProtocol(protocol Protocol) bool

	IsProxy() bool

	Retain() Object
	Release()
	Autorelease() Object
	RetainCount() uint

	Dealloc()
	Description() string
}

An interface definition for the Object class.

type IProtocol

type IProtocol interface {
	Handle
	Name() string
	MethodDescription(sel Selector, required bool, instanceMethod bool) MethodDescription
	CopyMethodDescriptionList(required bool, instanceMethod bool) []MethodDescription
	CopyProtocolList() []Protocol
	Property(name string, required bool, isInstanceProperty bool) Property
	CopyPropertyList() []Property
}

type Integer

type Integer = int64

type Ivar

type Ivar struct {
	// contains filtered or unexported fields
}

func (Ivar) Ptr

func (i Ivar) Ptr() unsafe.Pointer

type Method

type Method struct {
	// contains filtered or unexported fields
}

func (Method) Implementation

func (m Method) Implementation() IMP

func (Method) Name

func (m Method) Name() Selector

func (Method) Ptr

func (m Method) Ptr() unsafe.Pointer

func (Method) SetImplementation

func (m Method) SetImplementation(imp IMP) IMP

func (Method) TypeEncoding

func (m Method) TypeEncoding() string

type MethodDescription

type MethodDescription struct {
	Name  Selector
	Types string
}

type Object

type Object struct {
	// contains filtered or unexported fields
}

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects. Full Topic

func GetAssociatedObject

func GetAssociatedObject(o IObject, key unsafe.Pointer) Object

Returns the value associated with a given object for a given key. Full Topic

func NewObject

func NewObject() Object

Instantiate a new Object with Autorelease called

func ObjectFrom

func ObjectFrom(ptr unsafe.Pointer) Object

Make an Object from an unsafe.Pointer

func WrapAsProtocol

func WrapAsProtocol[T any](protocolName string, d T) Object

func (Object) Autorelease

func (o Object) Autorelease() Object

Decrements the receiver’s retain count at the end of the current autorelease pool block. Full Topic

func (Object) Class

func (o Object) Class() Class

Returns the class object for the receiver’s class. Full Topic

func (Object) ConformsToProtocol

func (o Object) ConformsToProtocol(protocol Protocol) bool

Returns a Boolean value that indicates whether the receiver conforms to a given protocol. Full Topic

func (Object) Dealloc

func (o Object) Dealloc()

Deallocates the memory occupied by the receiver. Full Topic

func (Object) Description

func (o Object) Description() string

Returns a string that represents the contents of the receiving class. Full Topic

func (Object) IsKindOfClass

func (o Object) IsKindOfClass(class Class) bool

Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class. Full Topic

func (Object) IsMemberOfClass

func (o Object) IsMemberOfClass(class Class) bool

Returns a Boolean value that indicates whether the receiver is an instance of a given class. Full Topic

func (Object) IsNil

func (o Object) IsNil() bool

IsNil returns true if the underlying pointer is nil

func (Object) IsProxy

func (o Object) IsProxy() bool

Returns a Boolean value that indicates whether the receiver does not descend from NSObject. Full Topic

func (Object) PerformSelector

func (o Object) PerformSelector(sel Selector) Object

Sends a specified message to the receiver and returns the result of the message. Full Topic

func (Object) PerformSelectorWithObject

func (o Object) PerformSelectorWithObject(sel Selector, object Object) Object

Sends a message to the receiver with an object as the argument. Full Topic

func (Object) PerformSelectorWithObjectWithObject

func (o Object) PerformSelectorWithObjectWithObject(sel Selector, obj1, obj2 Object) Object

Sends a message to the receiver with two objects as arguments. Full Topic

func (Object) Ptr

func (o Object) Ptr() unsafe.Pointer

Ptr returns the underlying unsafe.Pointer

func (Object) Release

func (o Object) Release()

Decrements the receiver’s reference count. Full Topic

func (Object) RespondsToSelector

func (o Object) RespondsToSelector(sel Selector) bool

Returns a Boolean value that indicates whether the receiver implements or inherits a method that can respond to a specified message. Full Topic

func (Object) Retain

func (o Object) Retain() Object

Increments the receiver’s reference count. Full Topic

func (Object) RetainCount

func (o Object) RetainCount() uint

"Do not use this method." Full Topic

type Property

type Property struct {
	// contains filtered or unexported fields
}

func (Property) Attributes

func (p Property) Attributes() string

func (Property) CopyAttributeList

func (p Property) CopyAttributeList() []PropertyAttribute

func (Property) CopyAttributeValue

func (p Property) CopyAttributeValue(name string) string

func (Property) Name

func (p Property) Name() string

func (Property) Ptr

func (p Property) Ptr() unsafe.Pointer

type PropertyAttribute

type PropertyAttribute struct {
	Name  string
	Value string
}

type Protocol

type Protocol struct {
	// contains filtered or unexported fields
}

func AllocateProtocol

func AllocateProtocol(name string) Protocol

func GetProtocol

func GetProtocol(name string) Protocol

func (Protocol) CopyMethodDescriptionList

func (p Protocol) CopyMethodDescriptionList(required bool, instanceMethod bool) []MethodDescription

func (Protocol) CopyPropertyList

func (p Protocol) CopyPropertyList() []Property

func (Protocol) CopyProtocolList

func (p Protocol) CopyProtocolList() []Protocol

func (Protocol) MethodDescription

func (p Protocol) MethodDescription(sel Selector, required bool, instanceMethod bool) MethodDescription

func (Protocol) Name

func (p Protocol) Name() string

func (Protocol) Property

func (p Protocol) Property(name string, required bool, isInstanceProperty bool) Property

func (Protocol) Ptr

func (p Protocol) Ptr() unsafe.Pointer

type Selector

type Selector struct {
	// contains filtered or unexported fields
}

func RegisterSelectorName

func RegisterSelectorName(name string) Selector

RegisterSelectorName registers a method with the Objective-C runtime system, maps the method name to a selector, and returns the selector value.

func Sel

func Sel(selName string) Selector

Sel returns a cached method selector by name, registering it if needed.

func SelectorFrom

func SelectorFrom(ptr unsafe.Pointer) Selector

func (Selector) Name

func (s Selector) Name() string

func (Selector) Ptr

func (s Selector) Ptr() unsafe.Pointer

type SyncCache

type SyncCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func (*SyncCache[K, V]) Load

func (c *SyncCache[K, V]) Load(key K, Compute func(key K) V) V

type UInteger

type UInteger = uint64

type UserClass

type UserClass[T IObject] struct {
	Class
}

UserClass is a generic wrapper around Class returned by NewClass.

func NewClass

func NewClass[T IObject](selectors ...Selector) UserClass[T]

NewClass will allocate a new class using the name of the type passed and NSObject as the superclass unless the passed type has a struct tag with the key "objc" specifying the name of the superclass to use. The returned class will still need to be registered.

func (UserClass[T]) New

func (c UserClass[T]) New() T

New creates an instance of the class then calls init and autorelease before returning.

type Void

type Void struct{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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