The Objective-C Runtime Reference
The Objective-C Runtime Reference
The Objective-C Runtime Reference
2009-10-19
ASSUMING THE ENTIRE RISK AS TO ITS QUALITY
AND ACCURACY.
Apple Inc.
IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT,
© 2002, 2009 Apple Inc. INDIRECT, SPECIAL, INCIDENTAL, OR
All rights reserved. CONSEQUENTIAL DAMAGES RESULTING FROM ANY
DEFECT OR INACCURACY IN THIS DOCUMENT, even
if advised of the possibility of such damages.
No part of this publication may be reproduced, THE WARRANTY AND REMEDIES SET FORTH ABOVE
stored in a retrieval system, or transmitted, in ARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORAL
any form or by any means, mechanical, OR WRITTEN, EXPRESS OR IMPLIED. No Apple
dealer, agent, or employee is authorized to make
electronic, photocopying, recording, or any modification, extension, or addition to this
otherwise, without prior written permission of warranty.
Apple Inc., with the following exceptions: Any Some states do not allow the exclusion or limitation
person is hereby authorized to store of implied warranties or liability for incidental or
consequential damages, so the above limitation or
documentation on a single computer for exclusion may not apply to you. This warranty gives
personal use only and to print copies of you specific legal rights, and you may also have
other rights which vary from state to state.
documentation for personal use provided that
the documentation contains Apple’s copyright
notice.
The Apple logo is a trademark of Apple Inc.
Use of the “keyboard” Apple logo
(Option-Shift-K) for commercial purposes
without the prior written consent of Apple may
constitute trademark infringement and unfair
competition in violation of federal and state
laws.
No licenses, express or implied, are granted
with respect to any of the technology described
in this document. Apple retains all intellectual
property rights associated with the technology
described in this document. This document is
intended to assist application developers to
develop applications only for Apple-labeled
computers.
Every effort has been made to ensure that the
information in this document is accurate. Apple
is not responsible for typographical errors.
Apple Inc.
1 Infinite Loop
Cupertino, CA 95014
408-996-1010
Overview 9
Who Should Read This Document 9
Functions by Task 10
Working with Classes 10
Adding Classes 11
Instantiating Classes 11
Working with Instances 11
Obtaining Class Definitions 12
Working with Instance Variables 12
Associative References 13
Sending Messages 13
Forwarding Messages 13
Working with Methods 14
Working with Selectors 14
Working with Protocols 15
Working with Properties 15
Functions 15
class_addIvar 15
class_addMethod 16
class_addProtocol 17
class_conformsToProtocol 17
class_copyIvarList 17
class_copyMethodList 18
class_copyPropertyList 18
class_copyProtocolList 19
class_createInstance 19
class_getClassMethod 20
class_getClassVariable 20
class_getInstanceMethod 20
class_getInstanceSize 21
class_getInstanceVariable 21
class_getIvarLayout 21
class_getMethodImplementation 22
class_getMethodImplementation_stret 22
class_getName 23
class_getProperty 23
class_getSuperclass 23
class_getVersion 24
class_getWeakIvarLayout 24
class_isMetaClass 24
3
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
CONTENTS
class_replaceMethod 25
class_respondsToSelector 25
class_setIvarLayout 26
class_setSuperclass 26
class_setVersion 26
class_setWeakIvarLayout 27
ivar_getName 27
ivar_getOffset 27
ivar_getTypeEncoding 28
marg_free 28
marg_getRef 28
marg_getValue 29
marg_malloc 29
marg_setValue 29
method_copyArgumentType 30
method_copyReturnType 30
method_exchangeImplementations 30
method_getArgumentType 31
method_getImplementation 31
method_getName 31
method_getNumberOfArguments 32
method_getReturnType 32
method_getTypeEncoding 32
method_setImplementation 33
objc_allocateClassPair 33
objc_copyProtocolList 33
objc_duplicateClass 34
objc_getAssociatedObject 34
objc_getClass 34
objc_getClassList 35
objc_getFutureClass 36
objc_getMetaClass 36
objc_getProtocol 36
objc_getRequiredClass 36
objc_lookUpClass 37
objc_msgSend 37
objc_msgSendSuper 38
objc_msgSendSuper_stret 38
objc_msgSend_fpret 39
objc_msgSend_stret 39
objc_registerClassPair 40
objc_removeAssociatedObjects 40
objc_setAssociatedObject 40
objc_setFutureClass 41
object_copy 41
object_dispose 41
4
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
CONTENTS
object_getClass 42
object_getClassName 42
object_getIndexedIvars 42
object_getInstanceVariable 43
object_getIvar 43
object_setClass 44
object_setInstanceVariable 44
object_setIvar 44
property_getAttributes 45
property_getName 45
protocol_conformsToProtocol 45
protocol_copyMethodDescriptionList 46
protocol_copyPropertyList 46
protocol_copyProtocolList 47
protocol_getMethodDescription 47
protocol_getName 48
protocol_getProperty 48
protocol_isEqual 49
sel_getName 49
sel_getUid 49
sel_isEqual 50
sel_registerName 50
Data Types 51
Class-Definition Data Structures 51
Instance Data Types 54
Boolean Value 55
Associative References 56
Constants 56
Boolean Values 56
Null Values 56
Associative Object Behaviors 57
Runtime Functions 59
Basic types 59
Instances 59
Class Inspection 60
Class Manipulation 61
Methods 61
Instance Variables 62
Selectors 62
Runtime 62
Messaging 63
Protocols 63
Exceptions 63
5
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
CONTENTS
Synchronization 64
NXHashTable and NXMapTable 64
Structures 64
6
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Tables and Listings
7
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
TABLES AND LISTINGS
8
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Declared in NSObjCRuntime.h
wintypes.h
Overview
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.”
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.
Overview 9
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Functions by Task
10 Functions by Task
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Adding Classes
objc_allocateClassPair (page 33)
Creates a new class and metaclass.
objc_registerClassPair (page 40)
Registers a class that was allocated using objc_allocateClassPair.
objc_duplicateClass (page 34)
Used by Foundation's Key-Value Observing.
Instantiating Classes
class_createInstance (page 19)
Creates an instance of a class, allocating memory for the class in the default malloc memory zone.
Functions by Task 11
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
12 Functions by Task
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Associative References
objc_setAssociatedObject (page 40)
Sets an associated value for a given object using a given key and association policy.
objc_getAssociatedObject (page 34)
Returns the value associated with a given object for a given key.
objc_removeAssociatedObjects (page 40)
Removes all associations for a given object.
Sending Messages
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 (page 37) sends a message with a simple return value to an instance of a class.
■ objc_msgSend_stret (page 39) sends a message with a data-structure return value to an instance of
a class.
■ objc_msgSendSuper (page 38) sends a message with a simple return value to the superclass of an
instance of a class.
■ objc_msgSendSuper_stret (page 38) sends a message with a data-structure return value to the
superclass of an instance of a class.
Forwarding Messages
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.
Functions by Task 13
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
14 Functions by Task
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Functions
class_addIvar
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)
Return Value
YES if the instance variable was added successfully, otherwise NO (for example, the class already contains an
instance variable with that name).
Functions 15
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
This function may only be called after objc_allocateClassPair (page 33) and before
objc_registerClassPair (page 40). 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)).
Declared In
runtime.h
class_addMethod
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)
Parameters
cls
The class to which to add a method.
name
A selector that specifies the name of the method being added.
imp
A function which is the implementation of the new method. The function must take at least two
arguments—self and _cmd.
types
An array of characters that describe the types of the arguments to the method. For possible values,
see Objective-C Runtime Programming Guide > 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).
Return Value
YES if the method was added successfully, otherwise NO (for example, the class already contains a method
implementation with that name).
Discussion
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 (page
33).
An Objective-C method is simply a C function that take at least two arguments—self and _cmd. For example,
given the following function:
you can dynamically add it to a class as a method (called resolveThisMethodDynamically) like this:
16 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Declared In
runtime.h
class_addProtocol
Adds a protocol to a class.
Parameters
cls
The class to modify.
outCount
The protocol to add to cls.
Return Value
YES if the method was added successfully, otherwise NO (for example, the class already conforms to that
protocol).
Declared In
runtime.h
class_conformsToProtocol
Returns a Boolean value that indicates whether a class conforms to a given protocol.
Parameters
cls
The class you want to inspect.
protocol
A protocol.
Return Value
YES if cls conforms to protocol, otherwise NO.
Discussion
You should usually use NSObject‘s conformsToProtocol: method instead of this function.
Declared In
runtime.h
class_copyIvarList
Describes the instance variables declared by a class.
Parameters
cls
The class to inspect.
Functions 17
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
outCount
On return, contains the length of the returned array. If outCount is NULL, the length is not returned.
Return Value
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.
Declared In
runtime.h
class_copyMethodList
Describes the instance methods implemented by a class.
Parameters
cls
The class you want to inspect.
outCount
On return, contains the length of the returned array. If outCount is NULL, the length is not returned.
Return Value
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.
Discussion
To get the class methods of a class, use class_copyMethodList(object_getClass(cls), &count).
Declared In
runtime.h
class_copyPropertyList
Describes the properties declared by a class.
Parameters
cls
The class you want to inspect.
outCount
On return, contains the length of the returned array. If outCount is NULL, the length is not returned.
18 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Return Value
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().
Declared In
runtime.h
class_copyProtocolList
Describes the protocols adopted by a class.
Parameters
cls
The class you want to inspect.
outCount
On return, contains the length of the returned array. If outCount is NULL, the length is not returned.
Return Value
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().
Declared In
runtime.h
class_createInstance
Creates an instance of a class, allocating memory for the class in the default malloc memory zone.
Parameters
cls
The class that you wish to allocate an instance of.
extraBytes
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.
Return Value
An instance of the class cls.
Declared In
runtime.h
Functions 19
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
class_getClassMethod
Returns a pointer to the data structure describing a given class method for a given class.
Parameters
aClass
A pointer to a class definition. Pass the class that contains the method you want to retrieve.
aSelector
A pointer of type SEL (page 52). Pass the selector of the method you want to retrieve.
Return Value
A pointer to the Method (page 51) 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.
Discussion
Note that this function searches superclasses for implementations, whereas class_copyMethodList (page
18) does not.
Declared In
runtime.h
class_getClassVariable
Returns the Ivar for a specified class variable of a given class.
Parameters
cls
The class definition whose class variable you wish to obtain.
name
The name of the class variable definition to obtain.
Return Value
A pointer to an Ivar (page 51) data structure containing information about the class variable specified by
name.
Declared In
runtime.h
class_getInstanceMethod
Returns a specified instance method for a given class.
Parameters
aClass
The class you want to inspect.
20 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
aSelector
The selector of the method you want to retrieve.
Return Value
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.
Discussion
Note that this function searches superclasses for implementations, whereas class_copyMethodList (page
18) does not.
Declared In
runtime.h
class_getInstanceSize
Returns the size of instances of a class.
Parameters
cls
A class object.
Return Value
The size in bytes of instances of the class cls, or 0 if cls is Nil.
Declared In
runtime.h
class_getInstanceVariable
Returns the Ivar for a specified instance variable of a given class.
Parameters
cls
The class whose instance variable you wish to obtain.
name
The name of the instance variable definition to obtain.
Return Value
A pointer to an Ivar (page 51) data structure containing information about the instance variable specified
by name.
Declared In
runtime.h
class_getIvarLayout
Returns a description of the Ivar layout for a given class.
Functions 21
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Parameters
cls
The class to inspect.
Return Value
A description of the Ivar layout for cls.
Declared In
runtime.h
class_getMethodImplementation
Returns the function pointer that would be called if a particular message were sent to an instance of a class.
Parameters
cls
The class you want to inspect.
name
A selector.
Return Value
The function pointer that would be called if [object name] were called with an instance of the class, or
NULL if cls is Nil.
Discussion
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.
Declared In
runtime.h
class_getMethodImplementation_stret
Returns the function pointer that would be called if a particular message were sent to an instance of a class.
Parameters
cls
The class you want to inspect.
name
A selector.
Return Value
The function pointer that would be called if [object name] were called with an instance of the class, or
NULL if cls is Nil.
22 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Declared In
runtime.h
class_getName
Returns the name of a class.
Parameters
cls
A class object.
Return Value
The name of the class, or the empty string if cls is Nil.
Declared In
runtime.h
class_getProperty
Returns a property with a given name of a given class.
Return Value
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.
Declared In
runtime.h
class_getSuperclass
Returns the superclass of a class.
Parameters
cls
A class object.
Return Value
The superclass of the class, or Nil if cls is a root class, or Nil if cls is Nil.
Discussion
You should usually use NSObject‘s superclass method instead of this function.
Declared In
runtime.h
Functions 23
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
class_getVersion
Returns the version number of a class definition.
Parameters
theClass
A pointer to an Class (page 51) data structure. Pass the class definition for which you wish to obtain
the version.
Return Value
An integer indicating the version number of the class definition.
Discussion
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.
Declared In
runtime.h
class_getWeakIvarLayout
Returns a description of the layout of weak Ivars for a given class.
Parameters
cls
The class to inspect.
Return Value
A description of the layout of the weak Ivars for cls.
Declared In
runtime.h
class_isMetaClass
Returns a Boolean value that indicates whether a class object is a metaclass.
Parameters
cls
A class object.
Return Value
YES if cls is a metaclass, NO if cls is a non-meta class, NO if cls is Nil.
24 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Declared In
runtime.h
class_replaceMethod
Replaces the implementation of a method for a given class.
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
Parameters
cls
The class you want to modify.
name
A selector that identifies the method whose implementation you want to replace.
imp
The new implementation for the method identified by name for the class identified by cls.
types
An array of characters that describe the types of the arguments to the method. For possible values,
see Objective-C Runtime Programming Guide > 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).
Return Value
The previous implementation of the method identified by name for the class identified by cls.
Discussion
This function behaves in two different ways:
■ If the method identified by name does not yet exist, it is added as if class_addMethod (page 16) were
called. The type encoding specified by types is used as given.
■ If the method identified by name does exist, its IMP is replaced as if method_setImplementation (page
33) were called. The type encoding specified by types is ignored.
Declared In
runtime.h
class_respondsToSelector
Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
Parameters
cls
The class you want to inspect.
sel
A selector.
Return Value
YES if instances of the class respond to the selector, otherwise NO.
Functions 25
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
You should usually use NSObject's respondsToSelector: or instancesRespondToSelector: methods
instead of this function.
Declared In
runtime.h
class_setIvarLayout
Sets the Ivar layout for a given class.
Parameters
cls
The class to modify.
layout
The layout of the Ivars for cls.
Declared In
runtime.h
class_setSuperclass
Sets the superclass of a given class.
Parameters
cls
The class whose superclass you want to set.
newSuper
The new superclass for cls.
Return Value
The old superclass for cls.
Special Considerations
You should not use this function.
Declared In
runtime.h
class_setVersion
Sets the version number of a class definition.
26 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Parameters
theClass
A pointer to an Class (page 51) data structure. Pass the class definition for which you wish to set
the version.
version
An integer. Pass the new version number of the class definition.
Discussion
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.
Declared In
runtime.h
class_setWeakIvarLayout
Sets the layout for weak Ivars for a given class.
Parameters
cls
The class to modify.
layout
The layout of the weak Ivars for cls.
Declared In
runtime.h
ivar_getName
Returns the name of an instance variable.
Return Value
A C string containing the instance variable's name.
Declared In
runtime.h
ivar_getOffset
Returns the offset of an instance variable.
Functions 27
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
For instance variables of type id or other object types, call object_getIvar (page 43) and
object_setIvar (page 44) instead of using this offset to access the instance variable data directly.
Declared In
runtime.h
ivar_getTypeEncoding
Returns the type string of an instance variable.
Return Value
A C string containing the instance variable's type encoding.
Discussion
For possible values, see Objective-C Runtime Programming Guide > Type Encodings.
Declared In
runtime.h
marg_free
Macro that releases an argument list.
marg_free(margs)
Parameters
margs
A pointer of type marg_list (page 52). Pass the argument list to release.
marg_getRef
Macro that returns a pointer to an argument in an argument list.
Parameters
margs
A pointer of type marg_list (page 52).
offset
A long integer value. Pass the byte offset to the argument in the list whose pointer you wish to obtain.
type
A type name. Pass the type of the argument located at offset.
Return Value
A pointer to the argument specified by the offset parameter.
28 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
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.
marg_getValue
Macro that returns the value of an argument in an argument list.
Parameters
margs
A pointer of type marg_list (page 52).
offset
A long integer value. Pass the byte offset to the argument in the list whose value you wish to obtain.
type
A type name. Pass the type of the argument located at offset.
Return Value
The value of the argument specified by the offset parameter.
Discussion
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.
marg_malloc
Macro that allocates an argument list.
marg_malloc(margs, method)
Parameters
margs
A pointer of type marg_list (page 52). Pass the variable that contains the argument list pointer.
method
A pointer to an Method (page 51) data structure. Pass the method for which the argument list is
allocated.
Discussion
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.
marg_setValue
Macro that sets the value of an argument in an argument list.
Parameters
margs
A pointer of type marg_list (page 52).
Functions 29
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
offset
A long integer value. Pass the byte offset to the argument in the list whose pointer you wish to obtain.
type
A type name. Pass the type of the argument located at offset.
value
A value. Pass the new value for the argument.
Discussion
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.
method_copyArgumentType
Returns a string describing a single parameter type of a method.
Parameters
method
The method to inspect.
index
The index of the parameter to inspect.
Return Value
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().
Declared In
runtime.h
method_copyReturnType
Returns a string describing a method's return type.
Parameters
method
The method to inspect.
Return Value
A C string describing the return type. You must free the string with free().
Declared In
runtime.h
method_exchangeImplementations
Exchanges the implementations of two methods.
30 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
This is an atomic version of the following:
Declared In
runtime.h
method_getArgumentType
Returns by reference a string describing a single parameter type of a method.
Discussion
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.
Declared In
runtime.h
method_getImplementation
Returns the implementation of a method.
Parameters
method
The method to inspect.
Return Value
A function pointer of type IMP.
Declared In
runtime.h
method_getName
Returns the name of a method.
Parameters
method
The method to inspect.
Functions 31
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Return Value
A pointer of type SEL.
Discussion
To get the method name as a C string, call sel_getName(method_getName(method)).
Declared In
runtime.h
method_getNumberOfArguments
Returns the number of arguments accepted by a method.
Parameters
method
A pointer to a Method (page 51) data structure. Pass the method in question.
Return Value
An integer containing the number of arguments accepted by the given method.
method_getReturnType
Returns by reference a string describing a method's return type.
Discussion
The method's return type string is copied to dst. dst is filled as if strncpy(dst, parameter_type,
dst_len) were called.
Declared In
runtime.h
method_getTypeEncoding
Returns a string describing a method's parameter and return types.
Parameters
method
The method to inspect.
Return Value
A C string. The string may be NULL.
Declared In
runtime.h
32 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
method_setImplementation
Sets the implementation of a method.
Return Value
The previous implementation of the method.
Declared In
runtime.h
objc_allocateClassPair
Creates a new class and metaclass.
Parameters
superclass
The class to use as the new class's superclass, or Nil to create a new root class.
name
The string to use as the new class's name. The string will be copied.
extraBytes
The number of bytes to allocate for indexed ivars at the end of the class and metaclass objects. This
should usually be 0.
Return Value
The new class, or Nil if the class could not be created (for example, the desired name is already in use).
Discussion
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 (page 16) and class_addIvar (page 15). When you are done building
the class, call objc_registerClassPair (page 40). 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.
Declared In
runtime.h
objc_copyProtocolList
Returns an array of all the protocols known to the runtime.
Parameters
outCount
Upon return, contains the number of protocols in the returned array.
Functions 33
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Return Value
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().
Discussion
This function acquires the runtime lock.
Declared In
runtime.h
objc_duplicateClass
Used by Foundation's Key-Value Observing.
objc_duplicateClass
Special Considerations
Do not call this function yourself.
Declared In
runtime.h
objc_getAssociatedObject
Returns the value associated with a given object for a given key.
Parameters
object
The source object for the association.
key
The key for the association.
Return Value
The value associated with the key key for object.
See Also
objc_setAssociatedObject (page 40)
objc_getClass
Returns the class definition of a specified class.
Parameters
name
The name of the class to look up.
Return Value
The Class object for the named class, or nil if the class is not registered with the Objective-C runtime.
34 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
objc_getClass is different from objc_lookUpClass (page 37) 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 (page 37) does not call the class handler callback.
Special Considerations
Earlier implementations of this function (prior to Mac OS X v10.0) terminate the program if the class does
not exist.
objc_getClassList
Obtains the list of registered class definitions.
Parameters
buffer
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.
bufferLen
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.
Return Value
An integer value indicating the total number of registered classes.
Discussion
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.
classes = NULL;
numClasses = objc_getClassList(NULL, 0);
if (numClasses > 0 )
{
classes = malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
free(classes);
}
Special Considerations
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.
Functions 35
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
objc_getFutureClass
Used by CoreFoundation's toll-free bridging.
Special Considerations
Do not call this function yourself.
Declared In
runtime.h
objc_getMetaClass
Returns the metaclass definition of a specified class.
Parameters
name
The name of the class to look up.
Return Value
The Class object for the metaclass of the named class, or nil if the class is not registered with the Objective-C
runtime.
Discussion
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.
objc_getProtocol
Returns a specified protocol.
Parameters
name
The name of a protocol.
Return Value
The protocol named name, or NULL if no protocol named name could be found.
Discussion
This function acquires the runtime lock.
Declared In
runtime.h
objc_getRequiredClass
Returns the class definition of a specified class.
36 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Parameters
name
The name of the class to look up.
Return Value
The Class object for the named class.
Discussion
This function is the same as objc_getClass (page 34), 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.
Declared In
runtime.h
objc_lookUpClass
Returns the class definition of a specified class.
Parameters
name
The name of the class to look up.
Return Value
The Class object for the named class, or nil if the class is not registered with the Objective-C runtime.
Discussion
objc_getClass (page 34) is different from this function in that if the class is not registered,
objc_getClass (page 34) 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.
objc_msgSend
Sends a message with a simple return value to an instance of a class.
Parameters
theReceiver
A pointer that points to the instance of the class that is to receive the message.
theSelector
The selector of the method that handles the message.
...
A variable argument list containing the arguments to the method.
Return Value
The return value of the method.
Functions 37
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
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.
objc_msgSendSuper
Sends a message with a simple return value to the superclass of an instance of a class.
Parameters
super
A pointer to an objc_super (page 55) 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.
op
A pointer of type SEL (page 52). Pass the selector of the method that will handle the message.
...
A variable argument list containing the arguments to the method.
Return Value
The return value of the method identified by op.
Discussion
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.
objc_msgSendSuper_stret
Sends a message with a data-structure return value to the superclass of an instance of a class.
Parameters
super
A pointer to an objc_super (page 55) 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.
op
A pointer of type SEL (page 52). Pass the selector of the method.
...
A variable argument list containing the arguments to the method.
38 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
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.
objc_msgSend_fpret
Sends a message with a floating-point return value to an instance of a class.
Parameters
self
A pointer that points to the instance of the class that is to receive the message.
op
The selector of the method that handles the message.
...
A variable argument list containing the arguments to the method.
Discussion
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.
Declared In
objc-runtime.h
objc_msgSend_stret
Sends a message with a data-structure return value to an instance of a class.
Parameters
stretAddr
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.
theReceiver
A pointer to the instance of the class that is to receive the message.
theSelector
A pointer of type SEL (page 52). Pass the selector of the method that handles the message.
...
A variable argument list containing the arguments to the method.
Functions 39
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
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.
objc_registerClassPair
Registers a class that was allocated using objc_allocateClassPair.
Parameters
cls
The class you want to register.
Declared In
runtime.h
objc_removeAssociatedObjects
Removes all associations for a given object.
Parameters
object
An object that maintains associated objects.
Discussion
The main purpose of this function is to make it easy to return an object to a "pristine state”. You should not
use this function for general removal of associations from objects, since it also removes associations that
other clients may have added to the object. Typically you should use objc_setAssociatedObject (page
40) with a nil value to clear an association.
See Also
objc_setAssociatedObject (page 40)
objc_getAssociatedObject (page 34)
objc_setAssociatedObject
Sets an associated value for a given object using a given key and association policy.
Parameters
object
The source object for the association.
key
The key for the association.
40 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
value
The value to associate with the key key for object. Pass nil to clear an existing association.
policy
The policy for the association. For possible values, see “Associative Object Behaviors” (page 57).
See Also
objc_setAssociatedObject (page 40)
objc_removeAssociatedObjects (page 40)
objc_setFutureClass
Used by CoreFoundation's toll-free bridging.
Special Considerations
Do not call this function yourself.
Declared In
runtime.h
object_copy
Returns a copy of a given object.
Parameters
obj
An Objective-C object.
size
The size of the object obj.
Return Value
A copy of obj.
Declared In
runtime.h
object_dispose
Frees the memory occupied by a given object.
id object_dispose(id obj)
Parameters
obj
An Objective-C object.
Return Value
nil.
Functions 41
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Declared In
runtime.h
object_getClass
Returns the class of an object.
Parameters
object
The object you want to inspect.
Return Value
The class object of which object is an instance, or Nil if object is nil.
Declared In
runtime.h
object_getClassName
Returns the class name of a given object.
Parameters
obj
An Objective-C object.
Return Value
The name of the class of which obj is an instance.
Declared In
runtime.h
object_getIndexedIvars
Returns a pointer to any extra bytes allocated with a instance given object.
Parameters
obj
An Objective-C object.
Return Value
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.
Discussion
This function returns a pointer to any extra bytes allocated with the instance (as specified by
class_createInstance (page 19) with extraBytes>0). This memory follows the object's ordinary ivars,
but may not be adjacent to the last ivar.
42 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
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.
Declared In
runtime.h
object_getInstanceVariable
Obtains the value of an instance variable of a class instance.
Parameters
obj
A pointer to an instance of a class. Pass the object containing the instance variable whose value you
wish to obtain.
name
A C string. Pass the name of the instance variable whose value you wish to obtain.
outValue
On return, contains a pointer to the value of the instance variable.
Return Value
A pointer to the Ivar (page 51) data structure that defines the type and name of the instance variable
specified by name.
Declared In
runtime.h
object_getIvar
Reads the value of an instance variable in an object.
Parameters
object
The object containing the instance variable whose value you want to read.
ivar
The Ivar describing the instance variable whose value you want to read.
Return Value
The value of the instance variable specified by ivar, or nil if object is nil.
Discussion
object_getIvar is faster than object_getInstanceVariable (page 43) if the Ivar for the instance
variable is already known.
Declared In
runtime.h
Functions 43
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
object_setClass
Sets the class of an object.
Parameters
object
The object to modify.
sel
A class object.
Return Value
The previous value of object‘s class, or Nil if object is nil.
Declared In
runtime.h
object_setInstanceVariable
Changes the value of an instance variable of a class instance.
Parameters
obj
A pointer to an instance of a class. Pass the object containing the instance variable whose value you
wish to modify.
name
A C string. Pass the name of the instance variable whose value you wish to modify.
value
The new value for the instance variable.
Return Value
A pointer to the Ivar (page 51) data structure that defines the type and name of the instance variable
specified by name.
Declared In
runtime.h
object_setIvar
Sets the value of an instance variable in an object.
Parameters
object
The object containing the instance variable whose value you want to set.
ivar
The Ivar describing the instance variable whose value you want to set.
44 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
value
The new value for the instance variable.
Discussion
object_setIvar is faster than object_setInstanceVariable (page 44) if the Ivar for the instance
variable is already known.
Declared In
runtime.h
property_getAttributes
Returns the attribute string of an property.
Return Value
A C string containing the property's attributes.
Discussion
The format of the attribute string is described in Declared Properties in Objective-C Runtime Programming
Guide.
Declared In
runtime.h
property_getName
Returns the name of a property.
Return Value
A C string containing the property's name.
Declared In
runtime.h
protocol_conformsToProtocol
Returns a Boolean value that indicates whether one protocol conforms to another protocol.
Parameters
proto
A protocol.
other
A protocol.
Return Value
YES if proto conforms to other, otherwise NO.
Functions 45
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Discussion
One protocol can incorporate other protocols using the same syntax that classes use to adopt a protocol:
All the protocols listed between angle brackets are considered part of the ProtocolName protocol.
Declared In
runtime.h
protocol_copyMethodDescriptionList
Returns an array of method descriptions of methods meeting a given specification for a given protocol.
Parameters
p
A protocol.
isRequiredMethod
A Boolean value that indicates whether returned methods should be required methods (pass YES to
specify required methods).
isInstanceMethod
A Boolean value that indicates whether returned methods should be instance methods (pass YES to
specify instance methods).
outCount
Upon return, contains the number of method description structures in the returned array.
Return Value
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.
Discussion
Methods in other protocols adopted by this protocol are not included.
Declared In
runtime.h
protocol_copyPropertyList
Returns an array of the properties declared by a protocol.
Parameters
proto
A protocol.
46 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
outCount
Upon return, contains the number of elements in the returned array.
Return Value
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().
Declared In
runtime.h
protocol_copyProtocolList
Returns an array of the protocols adopted by a protocol.
Parameters
proto
A protocol.
outCount
Upon return, contains the number of elements in the returned array.
Return Value
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().
Declared In
runtime.h
protocol_getMethodDescription
Returns a method description structure for a specified method of a given protocol.
Parameters
p
A protocol.
aSel
A selector
isRequiredMethod
A Boolean value that indicates whether aSel is a required method.
isInstanceMethod
A Boolean value that indicates whether aSel is an instance method.
Functions 47
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Return Value
An objc_method_description structure that describes the method specified by aSel, isRequiredMethod,
and isInstanceMethod for the protocol p.
If the protocol does not contain the specified method, returns an objc_method_description structure
with the value {NULL, NULL}.
Discussion
Methods in other protocols adopted by this protocol are not included.
Declared In
runtime.h
protocol_getName
Returns a the name of a protocol.
Parameters
p
A protocol.
Return Value
The name of the protocol p as a C string.
Declared In
runtime.h
protocol_getProperty
Returns the specified property of a given protocol.
Parameters
proto
A protocol.
name
The name of a property.
isRequiredProperty
A Boolean value that indicates whether name is a required property.
isInstanceProperty
A Boolean value that indicates whether name is a required property.
Return Value
The property specified by name, isRequiredProperty, and isInstanceProperty for proto, or NULL if
none of proto’s properties meets the specification.
Declared In
runtime.h
48 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
protocol_isEqual
Returns a Boolean value that indicates whether two protocols are equal.
Parameters
proto
A protocol.
other
A protocol.
Return Value
YES if proto is the same as other, otherwise NO.
Declared In
runtime.h
sel_getName
Returns the name of the method specified by a given selector.
Parameters
aSelector
A pointer of type SEL (page 52). Pass the selector whose name you wish to determine.
Return Value
A C string indicating the name of the selector.
Declared In
runtime.h
sel_getUid
Registers a method name with the Objective-C runtime system.
Parameters
str
A pointer to a C string. Pass the name of the method you wish to register.
Return Value
A pointer of type SEL (page 52) specifying the selector for the named method.
Discussion
The implementation of this method is identical to the implementation of sel_registerName (page 50).
Version Notes
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.
Functions 49
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Declared In
runtime.h
sel_isEqual
Returns a Boolean value that indicates whether two selectors are equal.
Parameters
lhs
The selector to compare with rhs.
rhs
The selector to compare with lhs.
Return Value
YES if rhs and rhs are equal, otherwise NO.
Discussion
sel_isEqual is equivalent to ==.
Declared In
runtime.h
sel_registerName
Registers a method with the Objective-C runtime system, maps the method name to a selector, and returns
the selector value.
Parameters
str
A pointer to a C string. Pass the name of the method you wish to register.
Return Value
A pointer of type SEL (page 52) specifying the selector for the named method.
Discussion
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.
Declared In
runtime.h
50 Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Data Types
Class
An opaque type that represents an Objective-C class.
Declared In
objc.h
Method
An opaque type that represents a method in a class definition.
Declared In
runtime.h
Ivar
An opaque type that represents an instance variable.
Declared In
runtime.h
Category
An opaque type that represents a category.
Declared In
runtime.h
objc_property_t
An opaque type that represents an Objective-C declared property.
Declared In
runtime.h
Data Types 51
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
IMP
A pointer to the start of a method implementation.
Discussion
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.
marg_list
A reference to an argument list.
Discussion
This data type is a reference to a list of method arguments. Use it with the functions described in “Working
with Instances” (page 11).
SEL
Defines an opaque type that represents a method selector.
Discussion
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 (page 50).
When using selectors, you must use the value returned from sel_registerName (page 50) or the Objective-C
compiler directive @selector(). You cannot simply cast a C string to SEL.
Declared In
objc.h
objc_method_list
Contains an array of method definitions.
52 Data Types
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
struct objc_method_list
{
struct objc_method_list *obsolete;
int method_count;
struct objc_method method_list[1];
}
Fields
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 (page 51) data structures.
objc_cache
Performance optimization for method calls. Contains pointers to recently used methods.
struct objc_cache
{
unsigned int mask;
unsigned int occupied;
Method buckets[1];
};
Fields
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 (page 51) 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.
Discussion
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.
objc_protocol_list
Represents a list of formal protocols.
Data Types 53
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
struct objc_protocol_list
{
struct objc_protocol_list *next;
int count;
Protocol *list[1];
};
Fields
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 (page 51) data structures that represent protocols.
Discussion
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.
objc_object
Represents an instance of a class.
struct objc_object
{
struct objc_class *isa;
/* …variable length data containing instance variable values… */
};
Fields
isa
A pointer to the class definition of which this object is an instance.
Discussion
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 (page 19) to create objc_object data structures.
54 Data Types
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
objc_super
Specifies the superclass of an instance.
struct objc_super
{
id receiver;
Class class;
};
Fields
receiver
A pointer of type id. Specifies an instance of a class.
class
A pointer to an Class (page 51) data structure. Specifies the particular superclass of the instance to
message.
Discussion
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.
Boolean Value
BOOL
Type to represent a Boolean value.
Discussion
BOOL is explicitly signed so @encode(BOOL) is c rather than C even if -funsigned-char is used.
Special Considerations
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();
Data Types 55
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Availability
Available in Mac OS X v10.1 and later.
Declared In
wintypes.h
Associative References
objc_AssociationPolicy
Type to specify behavior the behavior of an association.
Discussion
For values, see “Associative Object Behaviors” (page 57).
Constants
Boolean Values
These macros define convenient constants to represent Boolean values.
Constants
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.
Declared In
objc.h
Null Values
These macros define null values for classes and instances.
56 Constants
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
Constants
nil
Defines the id of a null instance.
Available in Mac OS X v10.0 through Mac OS X v10.4.
Declared in NSObjCRuntime.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.
Declared In
objc.h
enum {
OBJC_ASSOCIATION_ASSIGN = 0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
OBJC_ASSOCIATION_RETAIN = 01401,
OBJC_ASSOCIATION_COPY = 01403
};
Constants
OBJC_ASSOCIATION_ASSIGN
Specifies a weak reference to the associated object.
OBJC_ASSOCIATION_RETAIN_NONATOMIC
Specifies a strong reference to the associated object, and that the association is not made atomically.
OBJC_ASSOCIATION_COPY_NONATOMIC
Specifies that the associated object is copied, and that the association is not made atomically.
OBJC_ASSOCIATION_RETAIN
Specifies a strong reference to the associated object, and that the association is made atomically.
OBJC_ASSOCIATION_COPY
Specifies that the associated object is copied, and that the association is made atomically.
Constants 57
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
Objective-C Runtime Reference
58 Constants
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
APPENDIX A
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. This document describes the differences between
the 10.5 version and previous versions.
Runtime Functions
Basic types
arith_t: Changed from int to intptr_t.
Instances
The following functions are unchanged:
object_copy (page 41) (The nBytes parameter is changed from unsigned to size_t.)
Runtime Functions 59
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
APPENDIX A
Mac OS X Version 10.5 Delta
_copy: no substitute
_realloc: no substitute
_dealloc: no substitute
_zoneAlloc: no substitute
_zoneRealloc: no substitute
_zoneCopy: no substitute
_error: no substitute
Class Inspection
The following functions are unchanged:
60 Runtime Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
APPENDIX A
Mac OS X Version 10.5 Delta
objc_getRequiredClass
Class Manipulation
The following function is unchanged:
Methods
The following function is unchanged:
method_getArgumentInfo
method_getSizeOfArguments
Runtime Functions 61
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
APPENDIX A
Mac OS X Version 10.5 Delta
Instance Variables
The following functions are added:
Selectors
The following functions are unchanged:
Runtime
The following functions are deprecated favor of dyld:
objc_loadModules
objc_loadModule
objc_unloadModules
objc_getOrigClass
_objc_create_zone
_objc_error
_objc_flush_caches
_objc_resolve_categories_for_class
_objc_setClassLoader
_objc_setNilReceiver
_objc_getNilReceiver
_objcInit
62 Runtime Functions
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
APPENDIX A
Mac OS X Version 10.5 Delta
_objc_getFreedObjectClass
instrumentObjcMessageSends
_objc_debug_class_hash
_class_printDuplicateCacheEntries
_class_printMethodCaches
_class_printMethodCacheStatistics
Messaging
The following functions are unchanged:
objc_msgSendv
objc_msgSendv_stret
objc_msgSendv_fpret
Protocols
The following functions are added:
Exceptions
The following functions are unchanged:
objc_exception_throw
objc_exception_try_enter
objc_exception_try_exit
objc_exception_extract
objc_exception_match
objc_exception_get_functions
objc_exception_set_functions
Runtime Functions 63
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
APPENDIX A
Mac OS X Version 10.5 Delta
Synchronization
The following functions are unchanged:
objc_sync_enter
objc_sync_exit
objc_sync_wait
objc_sync_notify
objc_sync_notifyAll
Structures
struct objc_super {
id receiver;
Class super_class;
};
All other structures deprecated in favor of opaque types and functional API. Substitutes are shown in the
following tables.
Variable Substitution
64 Structures
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
APPENDIX A
Mac OS X Version 10.5 Delta
Variable Substitution
Variable Substitution
Variable Substitution
objc_object {...};
objc_category {...};
objc_method_list {...};
objc_ivar_list {...};
objc_protocol_list {...};
objc_cache {...};
objc_module {...};
objc_symtab {...};
Structures 65
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
APPENDIX A
Mac OS X Version 10.5 Delta
66 Structures
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
REVISION HISTORY
Date Notes
2008-11-19 Added links to the new Objective-C 2.0 Runtime Programming Guide.
2008-10-15 TBD
2007-10-31 Updated for Mac OS X v10.5. Corrected the code example for the
objc_getClassList function.
2004-08-31 New document that describes the data structures and programming interface
used in the Objective-C runtime system.
This document replaces information about the printing system that was
published previously in The Objective-C Programming Language.
67
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.
REVISION HISTORY
Document Revision History
68
2009-10-19 | © 2002, 2009 Apple Inc. All Rights Reserved.