Declared in | MacTypes.h NSObjCRuntime.h wintypes.h |
Companion guides |
This document describes the Mac OS X Objective-C 2.0 runtime library support functions and data structures. The functions are implemented in the shared library found at /usr/lib/libobjc.A.dylib
. This shared library provides support for the dynamic properties of the Objective-C language, and as such is linked to by all Objective-C applications.
This reference is useful primarily for developing bridge layers between Objective-C and other languages, or for low-level debugging. You typically do not need to use the Objective-C runtime library directly when programming in Objective-C.
The Mac OS X implementation of the Objective-C runtime library is unique to the Mac OS X platform. For other platforms, the GNU Compiler Collection provides a different implementation with a similar API. This document covers only the Mac OS X implementation.
The low-level Objective-C runtime API is significantly updated in Mac OS X version 10.5. Many functions and all existing data structures are replaced with new functions. The old functions and structures are deprecated in 32-bit and absent in 64-bit mode. The API constrains several values to 32-bit ints even in 64-bit mode—class count, protocol count, methods per class, ivars per class, arguments per method, sizeof(all arguments) per method, and class version number. In addition, the new Objective-C ABI (not described here) further constrains sizeof(anInstance)
to 32 bits, and three other values to 24 bits—methods per class, ivars per class, and sizeof(a single ivar). Finally, the obsolete NXHashTable
and NXMapTable
are limited to 4 billion items.
“Deprecated” below means “deprecated in Mac OS X version 10.5 for 32-bit code, and disallowed for 64-bit code.”
The document is intended for readers who might be interested in learning about the Objective-C runtime.
Because this isn’t a document about C, it assumes some prior acquaintance with that language. However, it doesn’t have to be an extensive acquaintance.
class_getName
class_getSuperclass
class_setSuperclass
class_isMetaClass
class_getInstanceSize
class_getInstanceVariable
class_getClassVariable
class_addIvar
class_copyIvarList
class_getIvarLayout
class_setIvarLayout
class_getWeakIvarLayout
class_setWeakIvarLayout
class_getProperty
class_copyPropertyList
class_addMethod
class_getInstanceMethod
class_getClassMethod
class_copyMethodList
class_replaceMethod
class_getMethodImplementation
class_getMethodImplementation_stret
class_respondsToSelector
class_addProtocol
class_conformsToProtocol
class_copyProtocolList
class_getVersion
class_setVersion
objc_getFutureClass
objc_setFutureClass
object_copy
object_dispose
object_setInstanceVariable
object_getInstanceVariable
object_getIndexedIvars
object_getIvar
object_setIvar
object_getClassName
object_getClass
object_setClass
When it encounters a method invocation, the compiler might generate a call to any of several functions to perform the actual message dispatch, depending on the receiver, the return value, and the arguments. You can use these functions to dynamically invoke methods from your own plain C code, or to use argument forms not permitted by NSObject’s perform...
methods. These functions are declared in /usr/include/objc/objc-runtime.h
.
objc_msgSend
sends a message with a simple return value to an instance of a class.
objc_msgSend_stret
sends a message with a data-structure return value to an instance of a class.
objc_msgSendSuper
sends a message with a simple return value to the superclass of an instance of a class.
objc_msgSendSuper_stret
sends a message with a data-structure return value to the superclass of an instance of a class.
This section describes the functions used by NSObject
and NSInvocation
to forward method invocations. The arguments to the method are given as a list of arguments, and as such the nature of the calling convention varies for each CPU architecture.
method_getName
method_getImplementation
method_getTypeEncoding
method_copyReturnType
method_copyArgumentType
method_getReturnType
method_getNumberOfArguments
method_getArgumentType
method_setImplementation
method_exchangeImplementations
objc_getProtocol
objc_copyProtocolList
protocol_getName
protocol_isEqual
protocol_copyMethodDescriptionList
protocol_getMethodDescription
protocol_copyPropertyList
protocol_getProperty
protocol_copyProtocolList
protocol_conformsToProtocol
Adds a new instance variable to a class.
BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types)
YES
if the instance variable was added successfully, otherwise NO
(for example, the class already contains an instance variable with that name).
This function may only be called after objc_allocateClassPair
and before objc_registerClassPair
. Adding an instance variable to an existing class is not supported.
The class must not be a metaclass. Adding an instance variable to a metaclass is not supported.
The instance variable's minimum alignment in bytes is 1<<align
. The minimum alignment of an instance variable depends on the ivar's type and the machine architecture. For variables of any pointer type, pass log2(sizeof(pointer_type))
.
runtime.h
Adds a new method to a class with a given name and implementation.
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
The class to which to add a method.
A selector that specifies the name of the method being added.
A function which is the implementation of the new method. The function must take at least two arguments—self
and _cmd
.
An array of characters that describe the types of the arguments to the method. For possible values, see The Objective-C 2.0 Programming Language > The Runtime System > “Type Encodings”. Since the function must take at least two arguments—self
and _cmd
, the second and third characters must be “@:
” (the first character is the return type).
YES
if the method was added successfully, otherwise NO
(for example, the class already contains a method implementation with that name).
class_addMethod
will add an override of a superclass's implementation, but will not replace an existing implementation in this class. To change an existing implementation, use method_setImplementation
.
An Objective-C method is simply a C function that take at least two arguments—self
and _cmd
. For example, given the following function:
void myMethodIMP(id self, SEL _cmd) |
{ |
// implementation .... |
} |
you can dynamically add it to a class as a method (called resolveThisMethodDynamically
) like this:
class_addMethod([self class], @selector(resolveThisMethodDynamically), (IMP) myMethodIMP, "v@:"); |
runtime.h
Adds a protocol to a class.
BOOL class_addProtocol(Class cls, Protocol *protocol)
The class to modify.
The protocol to add to cls.
YES
if the method was added successfully, otherwise NO
(for example, the class already conforms to that protocol).
runtime.h
Returns a Boolean value that indicates whether a class conforms to a given protocol.
BOOL class_conformsToProtocol(Class cls, Protocol *protocol)
The class you want to inspect.
A protocol.
YES
if cls conforms to protocol, otherwise NO
.
You should usually use NSObject
‘s conformsToProtocol:
method instead of this function.
runtime.h
Describes the instance variables declared by a class.
Ivar * class_copyIvarList(Class cls, unsigned int *outCount)
The class to inspect.
On return, contains the length of the returned array. If outCount is NULL
, the length is not returned.
An array of pointers of type Ivar describing the instance variables declared by the class. Any instance variables declared by superclasses are not included. The array contains *outCount
pointers followed by a NULL
terminator. You must free the array with free()
.
If the class declares no instance variables, or cls
is Nil
, NULL
is returned and *outCount
is 0
.
runtime.h
Describes the instance methods implemented by a class.
Method * class_copyMethodList(Class cls, unsigned int *outCount)
The class you want to inspect.
On return, contains the length of the returned array. If outCount is NULL
, the length is not returned.
An array of pointers of type Method
describing the instance methods implemented by the class—any instance methods implemented by superclasses are not included. The array contains *outCount
pointers followed by a NULL
terminator. You must free the array with free()
.
If cls implements no instance methods, or cls is Nil
, returns NULL
and *outCount
is 0
.
To get the class methods of a class, use class_copyMethodList(object_getClass(cls), &count)
.
To get the implementations of methods that may be implemented by superclasses, use class_getInstanceMethod
or class_getClassMethod
.
runtime.h
Describes the properties declared by a class.
objc_property_t * class_copyPropertyList(Class cls, unsigned int *outCount)
The class you want to inspect.
On return, contains the length of the returned array. If outCount is NULL
, the length is not returned.
An array of pointers of type objc_property_t
describing the properties declared by the class. Any properties declared by superclasses are not included. The array contains *outCount
pointers followed by a NULL
terminator. You must free the array with free()
.
If cls declares no properties, or cls is Nil
, returns NULL
and *outCount
is 0
.
runtime.h
Describes the protocols adopted by a class.
Protocol ** class_copyProtocolList(Class cls, unsigned int *outCount)
The class you want to inspect.
On return, contains the length of the returned array. If outCount is NULL
, the length is not returned.
An array of pointers of type Protocol*
describing the protocols adopted by the class. Any protocols adopted by superclasses or other protocols are not included. The array contains *outCount
pointers followed by a NULL
terminator. You must free the array with free()
.
If cls adopts no protocols, or cls is Nil
, returns NULL
and *outCount
is 0
.
runtime.h
Creates an instance of a class, allocating memory for the class in the default malloc memory zone.
id class_createInstance(Class cls, size_t extraBytes)
The class that you wish to allocate an instance of.
An integer indicating the number of extra bytes to allocate. The additional bytes can be used to store additional instance variables beyond those defined in the class definition.
An instance of the class cls.
runtime.h
Returns a pointer to the data structure describing a given class method for a given class.
Method class_getClassMethod(Class aClass, SEL aSelector)
A pointer to a class definition. Pass the class that contains the method you want to retrieve.
A pointer of type SEL
. Pass the selector of the method you want to retrieve.
A pointer to the Method
data structure that corresponds to the implementation of the selector specified by aSelector for the class specified by aClass, or NULL
if the specified class or its superclasses do not contain an instance method with the specified selector.
Note that this function searches superclasses for implementations, whereas class_copyMethodList
does not.
runtime.h
Returns the Ivar
for a specified class variable of a given class.
Ivar class_getClassVariable(Class cls, const char* name)
The class definition whose class variable you wish to obtain.
The name of the class variable definition to obtain.
A pointer to an Ivar
data structure containing information about the class variable specified by name
.
runtime.h
Returns a specified instance method for a given class.
Method class_getInstanceMethod(Class aClass, SEL aSelector)
The class you want to inspect.
The selector of the method you want to retrieve.
The method that corresponds to the implementation of the selector specified by aSelector for the class specified by aClass, or NULL
if the specified class or its superclasses do not contain an instance method with the specified selector.
Note that this function searches superclasses for implementations, whereas class_copyMethodList
does not.
runtime.h
Returns the size of instances of a class.
size_t class_getInstanceSize(Class cls)
A class object.
The size in bytes of instances of the class cls, or 0
if cls is Nil
.
runtime.h
Returns the Ivar
for a specified instance variable of a given class.
Ivar class_getInstanceVariable(Class cls, const char* name)
The class whose instance variable you wish to obtain.
The name of the instance variable definition to obtain.
A pointer to an Ivar
data structure containing information about the instance variable specified by name
.
runtime.h
Returns a description of the Ivar
layout for a given class.
const char *class_getIvarLayout(Class cls)
The class to inspect.
A description of the Ivar
layout for cls.
runtime.h
Returns the function pointer that would be called if a particular message were sent to an instance of a class.
IMP class_getMethodImplementation(Class cls, SEL name)
The class you want to inspect.
A selector.
The function pointer that would be called if [object name]
were called with an instance of the class, or NULL
if cls is Nil
.
class_getMethodImplementation
may be faster than method_getImplementation(class_getInstanceMethod(cls, name))
.
The function pointer returned may be a function internal to the runtime instead of an actual method implementation. For example, if instances of the class do not respond to the selector, the function pointer returned will be part of the runtime's message forwarding machinery.
runtime.h
Returns the function pointer that would be called if a particular message were sent to an instance of a class.
IMP class_getMethodImplementation_stret(Class cls, SEL name)
The class you want to inspect.
A selector.
The function pointer that would be called if [object name]
were called with an instance of the class, or NULL
if cls is Nil
.
runtime.h
Returns the name of a class.
const char * class_getName(Class cls)
A class object.
The name of the class, or the empty string if cls is Nil
.
runtime.h
Returns a property with a given name of a given class.
objc_property_t class_getProperty(Class cls, const char *name)
A pointer of type objc_property_t
describing the property, or NULL
if the class does not declare a property with that name, or NULL
if cls is Nil
.
runtime.h
Returns the superclass of a class.
Class class_getSuperclass(Class cls)
A class object.
The superclass of the class, or Nil
if cls is a root class, or Nil
if cls is Nil
.
You should usually use NSObject
‘s superclass
method instead of this function.
runtime.h
Returns the version number of a class definition.
int class_getVersion(Class theClass)
A pointer to an Class
data structure. Pass the class definition for which you wish to obtain the version.
An integer indicating the version number of the class definition.
You can use the version number of the class definition to provide versioning of the interface that your class represents to other classes. This is especially useful for object serialization (that is, archiving of the object in a flattened form), where it is important to recognize changes to the layout of the instance variables in different class-definition versions.
Classes derived from the Foundation framework NSObject
class can obtain the class-definition version number using the getVersion
class method, which is implemented using the class_getVersion
function.
runtime.h
Returns a description of the layout of weak Ivar
s for a given class.
const char *class_getWeakIvarLayout(Class cls)
The class to inspect.
A description of the layout of the weak Ivar
s for cls.
runtime.h
Returns a Boolean value that indicates whether a class object is a metaclass.
BOOL class_isMetaClass(Class cls)
A class object.
YES
if cls is a metaclass, NO
if cls is a non-meta class, NO
if cls is Nil
.
runtime.h
Replaces the implementation of a method for a given class.
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
The class you want to modify.
A selector that identifies the method whose implementation you want to replace.
The new implementation for the method identified by name for the class identified by cls.
An array of characters that describe the types of the arguments to the method. For possible values, see The Objective-C 2.0 Programming Language > The Runtime System > “Type Encodings”. Since the function must take at least two arguments—self
and _cmd
, the second and third characters must be “@:
” (the first character is the return type).
The previous implementation of the method identified by name for the class identified by cls.
runtime.h
Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
BOOL class_respondsToSelector(Class cls, SEL sel)
The class you want to inspect.
A selector.
YES
if instances of the class respond to the selector, otherwise NO
.
You should usually use NSObject
's respondsToSelector:
or instancesRespondToSelector:
methods instead of this function.
runtime.h
Sets the Ivar
layout for a given class.
void class_setIvarLayout(Class cls, const char *layout)
The class to modify.
The layout of the Ivar
s for cls.
runtime.h
Sets the superclass of a given class.
Class class_setSuperclass(Class cls, Class newSuper)
The class whose superclass you want to set.
The new superclass for cls.
The old superclass for cls.
You should not use this function.
runtime.h
Sets the version number of a class definition.
void class_setVersion(Class theClass, int version)
A pointer to an Class
data structure. Pass the class definition for which you wish to set the version.
An integer. Pass the new version number of the class definition.
You can use the version number of the class definition to provide versioning of the interface that your class represents to other classes. This is especially useful for object serialization (that is, archiving of the object in a flattened form), where it is important to recognize changes to the layout of the instance variables in different class-definition versions.
Classes derived from the Foundation framework NSObject
class can set the class-definition version number using the setVersion:
class method, which is implemented using the class_setVersion
function.
runtime.h
Sets the layout for weak Ivar
s for a given class.
void class_setWeakIvarLayout(Class cls, const char *layout)
The class to modify.
The layout of the weak Ivar
s for cls.
runtime.h
Returns the name of an instance variable.
const char * ivar_getName(Ivar ivar)
A C string containing the instance variable's name.
runtime.h
Returns the offset of an instance variable.
ptrdiff_t ivar_getOffset(Ivar ivar)
For instance variables of type id
or other object types, call object_getIvar
and object_setIvar
instead of using this offset to access the instance variable data directly.
runtime.h
Returns the type string of an instance variable.
const char * ivar_getTypeEncoding(Ivar ivar)
A C string containing the instance variable's type encoding.
runtime.h
Macro that releases an argument list.
marg_free(margs)
A pointer of type marg_list
. Pass the argument list to release.
Macro that returns a pointer to an argument in an argument list.
marg_getRef(margs, offset, type)
A pointer of type marg_list
.
A long integer value. Pass the byte offset to the argument in the list whose pointer you wish to obtain.
A type name. Pass the type of the argument located at offset
.
A pointer to the argument specified by the offset
parameter.
You can use this macro to manipulate any sort of int
or pointer parameter. If you want to handle floats and structs, you should use NSInvocation
instead.
Macro that returns the value of an argument in an argument list.
marg_getValue(margs, offset, type)
A pointer of type marg_list
.
A long integer value. Pass the byte offset to the argument in the list whose value you wish to obtain.
A type name. Pass the type of the argument located at offset
.
The value of the argument specified by the offset
parameter.
You can use this macro to manipulate any sort of int
or pointer parameter. If you want to handle floats and structs, you should use NSInvocation
instead.
Macro that allocates an argument list.
marg_malloc(margs, method)
A pointer of type marg_list
. Pass the variable that contains the argument list pointer.
A pointer to an Method
data structure. Pass the method for which the argument list is allocated.
You can use this macro to manipulate any sort of int
or pointer parameter. If you want to handle floats and structs, you should use NSInvocation
instead.
Macro that sets the value of an argument in an argument list.
marg_setValue(margs, offset, type, value)
A pointer of type marg_list
.
A long integer value. Pass the byte offset to the argument in the list whose pointer you wish to obtain.
A type name. Pass the type of the argument located at offset
.
A value. Pass the new value for the argument.
You can use this macro to manipulate any sort of int
or pointer parameter. If you want to handle floats and structs, you should use NSInvocation
instead.
Returns a string describing a single parameter type of a method.
char * method_copyArgumentType(Method method, unsigned int index)
The method to inspect.
The index of the parameter to inspect.
A C string describing the type of the parameter at index index, or NULL
if method has no parameter index index. You must free the string with free()
.
runtime.h
Returns a string describing a method's return type.
char * method_copyReturnType(Method method)
The method to inspect.
A C string describing the return type. You must free the string with free()
.
runtime.h
Exchanges the implementations of two methods.
void method_exchangeImplementations(Method m1, Method m2)
This is an atomic version of the following:
IMP imp1 = method_getImplementation(m1); |
IMP imp2 = method_getImplementation(m2); |
method_setImplementation(m1, imp2); |
method_setImplementation(m2, imp1); |
runtime.h
Returns by reference a string describing a single parameter type of a method.
void method_getArgumentType(Method method, unsigned int index, char *dst, size_t dst_len)
The parameter type string is copied to dst. dst is filled as if strncpy(dst, parameter_type, dst_len)
were called. If the method contains no parameter with that index, dst is filled as if strncpy(dst, "", dst_len)
were called.
runtime.h
Returns the implementation of a method.
IMP method_getImplementation(Method method)
The method to inspect.
A function pointer of type IMP
.
runtime.h
Returns the name of a method.
SEL method_getName(Method method)
The method to inspect.
A pointer of type SEL.
To get the method name as a C string, call sel_getName(method_getName(method))
.
runtime.h
Returns the number of arguments accepted by a method.
unsigned method_getNumberOfArguments(Method method)
A pointer to a Method
data structure. Pass the method in question.
An integer containing the number of arguments accepted by the given method.
Returns by reference a string describing a method's return type.
void method_getReturnType(Method method, char *dst, size_t dst_len)
The method's return type string is copied to dst. dst is filled as if strncpy(dst, parameter_type, dst_len)
were called.
runtime.h
Returns a string describing a method's parameter and return types.
const char * method_getTypeEncoding(Method method)
The method to inspect.
A C string. The string may be NULL
.
runtime.h
Sets the implementation of a method.
IMP method_setImplementation(Method method, IMP imp)
The previous implementation of the method.
runtime.h
Creates a new class and metaclass.
objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
The class to use as the new class's superclass, or Nil
to create a new root class.
The string to use as the new class's name. The string will be copied.
The number of bytes to allocate for indexed ivars at the end of the class and metaclass objects. This should usually be 0
.
The new class, or Nil
if the class could not be created (for example, the desired name is already in use).
You can get a pointer to the new metaclass by calling object_getClass(newClass)
.
To create a new class, start by calling objc_allocateClassPair
. Then set the class's attributes with functions like class_addMethod
and class_addIvar
. When you are done building the class, call objc_registerClassPair
. The new class is now ready for use.
Instance methods and instance variables should be added to the class itself. Class methods should be added to the metaclass.
runtime.h
Returns an array of all the protocols known to the runtime.
Protocol **objc_copyProtocolList(unsigned int *outCount)
Upon return, contains the number of protocols in the returned array.
A C array of all the protocols known to the runtime. The array contains *outCount
pointers followed by a NULL
terminator. You must free the list with free()
.
This function acquires the runtime lock.
runtime.h
Used by Foundation's Key-Value Observing.
objc_duplicateClass
Do not call this function yourself.
runtime.h
Returns the class definition of a specified class.
id objc_getClass(const char *name)
The name of the class to look up.
The Class object for the named class, or nil
if the class is not registered with the Objective-C runtime.
objc_getClass
is different from objc_lookUpClass
in that if the class is not registered, objc_getClass
calls the class handler callback and then checks a second time to see whether the class is registered. objc_lookUpClass
does not call the class handler callback.
Earlier implementations of this function (prior to Mac OS X v10.0) terminate the program if the class does not exist.
Obtains the list of registered class definitions.
int objc_getClassList(Class *buffer, int bufferLen)
An array of Class
values. On output, each Class
value points to one class definition, up to either bufferLen
or the total number of registered classes, whichever is less. You can pass NULL
to obtain the total number of registered class definitions without actually retrieving any class definitions.
An integer value. Pass the number of pointers for which you have allocated space in buffer
. On return, this function fills in only this number of elements. If this number is less than the number of registered classes, this function returns an arbitrary subset of the registered classes.
An integer value indicating the total number of registered classes.
The Objective-C runtime library automatically registers all the classes defined in your source code. You can create class definitions at runtime and register them with the objc_addClass
function.
Listing 1 demonstrates how to use this function to retrieve all the class definitions that have been registered with the Objective-C runtime in the current process.
Listing 1 Using objc_getClassList
int numClasses; |
Class * classes = NULL; |
classes = NULL; |
numClasses = objc_getClassList(NULL, 0); |
if (numClasses > 0 ) |
{ |
classes = malloc(sizeof(Class) * numClasses); |
numClasses = objc_getClassList(classes, numClasses); |
free(classes); |
} |
You cannot assume that class objects you get from this function are classes that inherit from NSObject
, so you cannot safely call any methods on such classes without detecting that the method is implemented first.
Used by CoreFoundation's toll-free bridging.
Class objc_getFutureClass(const char *name)
Do not call this function yourself.
runtime.h
Returns the metaclass definition of a specified class.
id objc_getMetaClass(const char *name)
The name of the class to look up.
The Class
object for the metaclass of the named class, or nil
if the class is not registered with the Objective-C runtime.
If the definition for the named class is not registered, this function calls the class handler callback and then checks a second time to see if the class is registered. However, every class definition must have a valid metaclass definition, and so the metaclass definition is always returned, whether it’s valid or not.
Returns a specified protocol.
Protocol *objc_getProtocol(const char *name)
The name of a protocol.
The protocol named name, or NULL
if no protocol named name could be found.
This function acquires the runtime lock.
runtime.h
Returns the class definition of a specified class.
id objc_getRequiredClass(const char *name)
The name of the class to look up.
The Class object for the named class.
This function is the same as objc_getClass
, but kills the process if the class is not found.
This function is used by ZeroLink, where failing to find a class would be a compile-time link error without ZeroLink.
runtime.h
Returns the class definition of a specified class.
id objc_lookUpClass(const char *name)
The name of the class to look up.
The Class object for the named class, or nil
if the class is not registered with the Objective-C runtime.
objc_getClass
is different from this function in that if the class is not registered, objc_getClass
calls the class handler callback and then checks a second time to see whether the class is registered. This function does not call the class handler callback.
Sends a message with a simple return value to an instance of a class.
id objc_msgSend(id theReceiver, SEL theSelector, ...)
A pointer that points to the instance of the class that is to receive the message.
The selector of the method that handles the message.
A variable argument list containing the arguments to the method.
The return value of the method.
When it encounters a method call, the compiler generates a call to one of the functions objc_msgSend
, objc_msgSend_stret
, objc_msgSendSuper
, or objc_msgSendSuper_stret
. Messages sent to an object’s superclass (using the super
keyword) are sent using objc_msgSendSuper
; other messages are sent using objc_msgSend
. Methods that have data structures as return values are sent using objc_msgSendSuper_stret
and objc_msgSend_stret
.
Sends a message with a simple return value to the superclass of an instance of a class.
id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
A pointer to an objc_super
data structure. Pass values identifying the context the message was sent to, including the instance of the class that is to receive the message and the superclass at which to start searching for the method implementation.
A pointer of type SEL
. Pass the selector of the method that will handle the message.
A variable argument list containing the arguments to the method.
The return value of the method identified by op.
When it encounters a method call, the compiler generates a call to one of the functions objc_msgSend
, objc_msgSend_stret
, objc_msgSendSuper
, or objc_msgSendSuper_stret
. Messages sent to an object’s superclass (using the super
keyword) are sent using objc_msgSendSuper
; other messages are sent using objc_msgSend
. Methods that have data structures as return values are sent using objc_msgSendSuper_stret
and objc_msgSend_stret
.
Sends a message with a data-structure return value to the superclass of an instance of a class.
void objc_msgSendSuper_stret(struct objc_super *super, SEL op, ...)
A pointer to an objc_super
data structure. Pass values identifying the context the message was sent to, including the instance of the class that is to receive the message and the superclass at which to start searching for the method implementation.
A pointer of type SEL
. Pass the selector of the method.
A variable argument list containing the arguments to the method.
When it encounters a method call, the compiler generates a call to one of the functions objc_msgSend
, objc_msgSend_stret
, objc_msgSendSuper
, or objc_msgSendSuper_stret
. Messages sent to an object’s superclass (using the super
keyword) are sent using objc_msgSendSuper
; other messages are sent using objc_msgSend
. Methods that have data structures as return values are sent using objc_msgSendSuper_stret
and objc_msgSend_stret
.
Sends a message with a floating-point return value to an instance of a class.
double objc_msgSend_fpret(id self, SEL op, ...)
A pointer that points to the instance of the class that is to receive the message.
The selector of the method that handles the message.
A variable argument list containing the arguments to the method.
On the i386 platform, the ABI for functions returning a floating-point value is incompatible with that for functions returning an integral type. On the i386 platform, therefore, you must use objc_msgSend_fpret
for functions that for functions returning non-integral type. For float
or long double
return types, cast the function to an appropriate function pointer type first.
This function is not used on the PPC or PPC64 platforms.
objc-runtime.h
Sends a message with a data-structure return value to an instance of a class.
void objc_msgSend_stret(void * stretAddr, id theReceiver, SEL theSelector, ...)
On input, a pointer that points to a block of memory large enough to contain the return value of the method. On output, contains the return value of the method.
A pointer to the instance of the class that is to receive the message.
A pointer of type SEL
. Pass the selector of the method that handles the message.
A variable argument list containing the arguments to the method.
When it encounters a method call, the compiler generates a call to one of the functions objc_msgSend
, objc_msgSend_stret
, objc_msgSendSuper
, or objc_msgSendSuper_stret
. Messages sent to an object’s superclass (using the super
keyword) are sent using objc_msgSendSuper
; other messages are sent using objc_msgSend
. Methods that have data structures as return values are sent using objc_msgSendSuper_stret
and objc_msgSend_stret
.
Registers a class that was allocated using objc_allocateClassPair
.
void objc_registerClassPair(Class cls)
The class you want to register.
runtime.h
Used by CoreFoundation's toll-free bridging.
void objc_setFutureClass(Class cls, const char *name)
Do not call this function yourself.
runtime.h
Returns a copy of a given object.
id object_copy(id obj, size_t size)
An Objective-C object.
The size of the object obj.
A copy of obj.
runtime.h
Frees the memory occupied by a given object.
id object_dispose(id obj)
An Objective-C object.
nil
.
runtime.h
Returns the class of an object.
Class object_getClass(id object)
The object you want to inspect.
The class object of which object is an instance, or Nil
if object is nil
.
runtime.h
Returns the class name of a given object.
const char *object_getClassName(id obj)
An Objective-C object.
The name of the class of which obj is an instance.
runtime.h
Returns a pointer to any extra bytes allocated with a instance given object.
OBJC_EXPORT void *object_getIndexedIvars(id obj)
An Objective-C object.
A pointer to any extra bytes allocated with obj. If obj was not allocated with any extra bytes, then dereferencing the returned pointer is undefined.
This function returns a pointer to any extra bytes allocated with the instance (as specified by class_createInstance
with extraBytes>0). This memory follows the object's ordinary ivars, but may not be adjacent to the last ivar.
The returned pointer is guaranteed to be pointer-size aligned, even if the area following the object's last ivar is less aligned than that. Alignment greater than pointer-size is never guaranteed, even if the area following the object's last ivar is more aligned than that.
In a garbage-collected environment, the memory is scanned conservatively.
runtime.h
Obtains the value of an instance variable of a class instance.
Ivar object_getInstanceVariable(id obj, const char *name, void **outValue)
A pointer to an instance of a class. Pass the object containing the instance variable whose value you wish to obtain.
A C string. Pass the name of the instance variable whose value you wish to obtain.
On return, contains a pointer to the value of the instance variable.
A pointer to the Ivar
data structure that defines the type and name of the instance variable specified by name
.
runtime.h
Reads the value of an instance variable in an object.
id object_getIvar(id object, Ivar ivar)
The object containing the instance variable whose value you want to read.
The Ivar describing the instance variable whose value you want to read.
The value of the instance variable specified by ivar, or nil
if object is nil
.
object_getIvar
is faster than object_getInstanceVariable
if the Ivar for the instance variable is already known.
runtime.h
Sets the class of an object.
Class object_setClass(id object, Class cls)
The object to modify.
A class object.
The previous value of object‘s class, or Nil
if object is nil
.
runtime.h
Changes the value of an instance variable of a class instance.
Ivar object_setInstanceVariable(id obj, const char *name, void *value)
A pointer to an instance of a class. Pass the object containing the instance variable whose value you wish to modify.
A C string. Pass the name of the instance variable whose value you wish to modify.
The new value for the instance variable.
A pointer to the Ivar
data structure that defines the type and name of the instance variable specified by name
.
runtime.h
Sets the value of an instance variable in an object.
void object_setIvar(id object, Ivar ivar, id value)
The object containing the instance variable whose value you want to set.
The Ivar describing the instance variable whose value you want to set.
The new value for the instance variable.
object_setIvar
is faster than object_setInstanceVariable
if the Ivar for the instance variable is already known.
runtime.h
Returns the attribute string of an property.
const char *property_getAttributes(objc_property_t property)
A C string containing the property's attributes.
The format of the attribute string is described in Declared Properties in Objective-C 2.0 Runtime Programming Guide.
runtime.h
Returns the name of a property.
const char *property_getName(objc_property_t property)
A C string containing the property's name.
runtime.h
Returns a Boolean value that indicates whether one protocol conforms to another protocol.
BOOL protocol_conformsToProtocol(Protocol *proto, Protocol *other)
A protocol.
A protocol.
YES
if proto conforms to other, otherwise NO
.
One protocol can incorporate other protocols using the same syntax that classes use to adopt a protocol:
@protocol ProtocolName < protocol list > |
All the protocols listed between angle brackets are considered part of the ProtocolName protocol.
runtime.h
Returns an array of method descriptions of methods meeting a given specification for a given protocol.
struct objc_method_description *protocol_copyMethodDescriptionList(Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount)
A protocol.
A Boolean value that indicates whether returned methods should be required methods (pass YES
to specify required methods).
A Boolean value that indicates whether returned methods should be instance methods (pass YES
to specify required methods).
Upon return, contains the number of method description structures in the returned array.
A C array of objc_method_description
structures containing the names and types of p’s methods specified by isRequiredMethod and isInstanceMethod. The array contains *outCount
pointers followed by a NULL
terminator. You must free the list with free()
.
If the protocol declares no methods that meet the specification, NULL
is returned and *outCount
is 0
.
Methods in other protocols adopted by this protocol are not included.
runtime.h
Returns an array of the properties declared by a protocol.
objc_property_t * protocol_copyPropertyList(Protocol *protocol, unsigned int *outCount)
A protocol.
Upon return, contains the number of elements in the returned array.
A C array of pointers of type objc_property_t
describing the properties declared by proto. Any properties declared by other protocols adopted by this protocol are not included. The array contains *outCount
pointers followed by a NULL
terminator. You must free the array with free()
.
If the protocol declares no properties, NULL
is returned and *outCount
is 0
.
runtime.h
Returns an array of the protocols adopted by a protocol.
Protocol **protocol_copyProtocolList(Protocol *proto, unsigned int *outCount)
A protocol.
Upon return, contains the number of elements in the returned array.
A C array of protocols adopted by proto. The array contains *outCount
pointers followed by a NULL
terminator. You must free the array with free()
.
If the protocol declares no properties, NULL
is returned and *outCount
is 0
.
runtime.h
Returns a method description structure for a specified method of a given protocol.
struct objc_method_description protocol_getMethodDescription(Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod)
A protocol.
A selector
A Boolean value that indicates whether aSel is a required method.
A Boolean value that indicates whether aSel is an instance method.
An objc_method_description
structure that describes the method specified by aSel, isRequiredMethod, and isInstanceMethod for the protocol p, or NULL
if p does not contain a method with that specification.
Methods in other protocols adopted by this protocol are not included.
runtime.h
Returns a the name of a protocol.
const char *protocol_getName(Protocol *p)
A protocol.
The name of the protocol p as a C string.
runtime.h
Returns the specified property of a given protocol.
objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty)
A protocol.
The name of a property.
A Boolean value that indicates whether name is a required property.
A Boolean value that indicates whether name is a required property.
The property specified by name, isRequiredProperty, and isInstanceProperty for proto, or NULL
if none of proto’s properties meets the specification.
runtime.h
Returns a Boolean value that indicates whether two protocols are equal.
BOOL protocol_isEqual(Protocol *proto, Protocol *other)
A protocol.
A protocol.
YES
if proto is the same as other, otherwise NO
.
runtime.h
Returns the name of the method specified by a given selector.
const char* sel_getName(SEL aSelector)
A pointer of type SEL
. Pass the selector whose name you wish to determine.
A C string indicating the name of the selector.
runtime.h
Registers a method name with the Objective-C runtime system.
SEL sel_getUid(const char *str)
A pointer to a C string. Pass the name of the method you wish to register.
A pointer of type SEL
specifying the selector for the named method.
The implementation of this method is identical to the implementation of sel_registerName
.
Prior to Mac OS X version 10.0, this method tried to find the selector mapped to the given name and returned NULL
if the selector was not found. This was changed for safety, because it was observed that many of the callers of this function did not check the return value for NULL
.
runtime.h
Returns a Boolean value that indicates whether two selectors are equal.
BOOL sel_isEqual(SEL lhs, SEL rhs)
The selector to compare with rhs.
The selector to compare with lhs.
YES
if rhs and rhs are equal, otherwise NO
.
sel_isEqual
is equivalent to ==
.
runtime.h
Registers a method with the Objective-C runtime system, maps the method name to a selector, and returns the selector value.
SEL sel_registerName(const char *str)
A pointer to a C string. Pass the name of the method you wish to register.
A pointer of type SEL
specifying the selector for the named method.
You must register a method name with the Objective-C runtime system to obtain the method’s selector before you can add the method to a class definition. If the method name has already been registered, this function simply returns the selector.
runtime.h
An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;
objc.h
An opaque type that represents a method in a class definition.
typedef struct objc_method *Method;
runtime.h
An opaque type that represents an instance variable.
typedef struct objc_ivar *Ivar;
runtime.h
An opaque type that represents a category.
typedef struct objc_category *Category;
runtime.h
An opaque type that represents an Objective-C declared property.
typedef struct objc_property *objc_property_t;
runtime.h
A pointer to the start of a method implementation.
id (*IMP)(id, SEL, ...)
This data type is a pointer to the start of the function that implements the method. This function uses standard C calling conventions as implemented for the current CPU architecture. The first argument is a pointer to self
(that is, the memory for the particular instance of this class, or, for a class method, a pointer to the metaclass). The second argument is the method selector. The method arguments follow.
A reference to an argument list.
typedef void* marg_list;
This data type is a reference to a list of method arguments. Use it with the functions described in “Working with Instances.”
Defines an opaque type that represents a method selector.
typedef struct objc_selector *SEL;
Method selectors are used to represent the name of a method at runtime. A method selector is a C string that has been registered (or “mapped“) with the Objective-C runtime. Selectors generated by the compiler are automatically mapped by the runtime when the class is loaded.
You can add new selectors at runtime and retrieve existing selectors using the function sel_registerName
.
When using selectors, you must use the value returned from sel_registerName
or the Objective-C compiler directive @selector()
. You cannot simply cast a C string to SEL
.
objc.h
Contains an array of method definitions.
struct objc_method_list { struct objc_method_list *obsolete; int method_count; struct objc_method method_list[1]; }
obsolete
Reserved for future use.
method_count
An integer specifying the number of methods in the method list array.
method_list
An array of Method
data structures.
Performance optimization for method calls. Contains pointers to recently used methods.
struct objc_cache { unsigned int mask; unsigned int occupied; Method buckets[1]; };
mask
An integer specifying the total number of allocated cache buckets (minus one). During method lookup, the Objective-C runtime uses this field to determine the index at which to begin a linear search of the buckets
array. A pointer to a method’s selector is masked against this field using a logical AND operation (index = (mask & selector))
. This serves as a simple hashing algorithm.
occupied
An integer specifying the total number of occupied cache buckets.
buckets
An array of pointers to Method
data structures. This array may contain no more than mask + 1
items. Note that pointers may be NULL
, indicating that the cache bucket is unoccupied, and occupied buckets may not be contiguous. This array may grow over time.
To limit the need to perform linear searches of method lists for the definitions of frequently accessed methods—an operation that can considerably slow down method lookup—the Objective-C runtime functions store pointers to the definitions of the most recently called method of the class in an objc_cache
data structure.
Represents a list of formal protocols.
struct objc_protocol_list { struct objc_protocol_list *next; int count; Protocol *list[1]; };
next
A pointer to another objc_protocol_list
data structure.
count
The number of protocols in this list.
list
An array of pointers to Class
data structures that represent protocols.
A formal protocol is a class definition that declares a set of methods, which a class must implement. Such a class definition contains no instance variables. A class definition may promise to implement any number of formal protocols.
These are the data types that represent objects, classes, and superclasses.
id
pointer to an instance of a class.
objc_object
represents an instance of a class.
objc_super
specifies the superclass of an instance.
A pointer to an instance of a class.
typedef struct objc_object { Class isa; } *id;
objc.h
Represents an instance of a class.
struct objc_object { struct objc_class *isa; /* …variable length data containing instance variable values… */ };
isa
A pointer to the class definition of which this object is an instance.
When you create an instance of a particular class, the allocated memory contains an objc_object
data structure, which is directly followed by the data for the instance variables of the class.
The alloc
and allocWithZone:
methods of the Foundation framework class NSObject use the function class_createInstance
to create objc_object
data structures.
Specifies the superclass of an instance.
struct objc_super { id receiver; Class class; };
receiver
A pointer of type id
. Specifies an instance of a class.
class
A pointer to an Class
data structure. Specifies the particular superclass of the instance to message.
The compiler generates an objc_super
data structure when it encounters the super
keyword as the receiver of a message. It specifies the class definition of the particular superclass that should be messaged.
Type to represent a Boolean value.
typedef signed char BOOL;
BOOL
is explicitly signed so @encode(BOOL)
is c
rather than C
even if -funsigned-char
is used.
For values, see “Boolean Values”
.
Since the type of BOOL
is actually char
, it does not behave in the same way as a C _Bool
value or a C++ bool value. For example, the conditional in the following code will be false on i386 (and true on PPC):
- (BOOL)value { |
return 256; |
} |
// then |
if ([self value]) doStuff(); |
By contrast, the conditional in the following code will be true on all platforms (even where sizeof(bool) == 1
):
- (bool)value { |
return 256; |
} |
// then |
if ([self value]) doStuff(); |
wintypes.h
These macros define convenient constants to represent Boolean values.
#define YES (BOOL)1 #define NO (BOOL)0
YES
Defines YES
as 1
.
Available in Mac OS X v10.0 and later.
Declared in NSObjCRuntime.h
.
NO
Defines NO
as 0
.
Available in Mac OS X v10.0 and later.
Declared in NSObjCRuntime.h
.
objc.h
These macros define null values for classes and instances.
#define nil __DARWIN_NULL #define Nil __DARWIN_NULL
nil
Defines the id
of a null instance.
Available in Mac OS X v10.0 and later.
Declared in MacTypes.h
.
Nil
Defines the id
of a null class.
Available in Mac OS X v10.0 through Mac OS X v10.4.
Declared in NSObjCRuntime.h
.
objc.h
© 2002, 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-11-19)