iOS Developer Library — Pre-Release

Developer

Objective-C Runtime Reference

Options
Deployment Target:

On This Page
Language:

Objective-C Runtime Reference

Inheritance


Not Applicable

Conforms To


Not Applicable

Import Statement


Swift

import ObjectiveC

Objective-C

@import Foundation;

This document describes the 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 OS X implementation of the Objective-C runtime library is unique to the Mac. For other platforms, the GNU Compiler Collection provides a different implementation with a similar API. This document covers only the OS X implementation.

The low-level Objective-C runtime API is significantly updated in 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 OS X version 10.5 for 32-bit code, and disallowed for 64-bit code.”

Who Should Read This Document

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.

Functions

  • Returns the name of a class.

    Declaration

    Swift

    func class_getName(_ cls: AnyClass!) -> UnsafePointer<Int8>

    Objective-C

    const char * class_getName ( Class cls );

    Parameters

    cls

    A class object.

    Return Value

    The name of the class, or the empty string if cls is Nil.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the superclass of a class.

    Declaration

    Swift

    func class_getSuperclass(_ cls: AnyClass!) -> AnyClass!

    Objective-C

    Class class_getSuperclass ( Class cls );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Sets the superclass of a given class.

    Declaration

    Objective-C

    Class class_setSuperclass ( Class cls, Class newSuper );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

    Deprecated in iOS 2.0.

  • Returns a Boolean value that indicates whether a class object is a metaclass.

    Declaration

    Swift

    func class_isMetaClass(_ cls: AnyClass!) -> Bool

    Objective-C

    BOOL class_isMetaClass ( Class cls );

    Parameters

    cls

    A class object.

    Return Value

    YEStrue if cls is a metaclass, NOfalse if cls is a non-meta class, NOfalse if cls is Nil.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the size of instances of a class.

    Declaration

    Swift

    func class_getInstanceSize(_ cls: AnyClass!) -> Int

    Objective-C

    size_t class_getInstanceSize ( Class cls );

    Parameters

    cls

    A class object.

    Return Value

    The size in bytes of instances of the class cls, or 0 if cls is Nil.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the Ivar for a specified instance variable of a given class.

    Declaration

    Swift

    func class_getInstanceVariable(_ cls: AnyClass!, _ name: UnsafePointer<Int8>) -> Ivar

    Objective-C

    Ivar class_getInstanceVariable ( Class cls, const char *name );

    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 data structure containing information about the instance variable specified by name.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the Ivar for a specified class variable of a given class.

    Declaration

    Swift

    func class_getClassVariable(_ cls: AnyClass!, _ name: UnsafePointer<Int8>) -> Ivar

    Objective-C

    Ivar class_getClassVariable ( Class cls, const char *name );

    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 data structure containing information about the class variable specified by name.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Adds a new instance variable to a class.

    Declaration

    Swift

    func class_addIvar(_ cls: AnyClass!, _ name: UnsafePointer<Int8>, _ size: Int, _ alignment: UInt8, _ types: UnsafePointer<Int8>) -> Bool

    Objective-C

    BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const char *types );

    Return Value

    YEStrue if the instance variable was added successfully, otherwise NOfalse (for example, the class already contains an instance variable with that name).

    Discussion

    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)).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Describes the instance variables declared by a class.

    Declaration

    Swift

    func class_copyIvarList(_ cls: AnyClass!, _ outCount: UnsafeMutablePointer<UInt32>) -> UnsafeMutablePointer<Ivar>

    Objective-C

    Ivar * class_copyIvarList ( Class cls, unsigned int *outCount );

    Parameters

    cls

    The class 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 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a description of the Ivar layout for a given class.

    Declaration

    Swift

    func class_getIvarLayout(_ cls: AnyClass!) -> UnsafePointer<UInt8>

    Objective-C

    const uint8_t * class_getIvarLayout ( Class cls );

    Parameters

    cls

    The class to inspect.

    Return Value

    A description of the Ivar layout for cls.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Sets the Ivar layout for a given class.

    Declaration

    Swift

    func class_setIvarLayout(_ cls: AnyClass!, _ layout: UnsafePointer<UInt8>)

    Objective-C

    void class_setIvarLayout ( Class cls, const uint8_t *layout );

    Parameters

    cls

    The class to modify.

    layout

    The layout of the Ivars for cls.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a description of the layout of weak Ivars for a given class.

    Declaration

    Swift

    func class_getWeakIvarLayout(_ cls: AnyClass!) -> UnsafePointer<UInt8>

    Objective-C

    const uint8_t * class_getWeakIvarLayout ( Class cls );

    Parameters

    cls

    The class to inspect.

    Return Value

    A description of the layout of the weak Ivars for cls.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Sets the layout for weak Ivars for a given class.

    Declaration

    Swift

    func class_setWeakIvarLayout(_ cls: AnyClass!, _ layout: UnsafePointer<UInt8>)

    Objective-C

    void class_setWeakIvarLayout ( Class cls, const uint8_t *layout );

    Parameters

    cls

    The class to modify.

    layout

    The layout of the weak Ivars for cls.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a property with a given name of a given class.

    Declaration

    Swift

    func class_getProperty(_ cls: AnyClass!, _ name: UnsafePointer<Int8>) -> objc_property_t

    Objective-C

    objc_property_t class_getProperty ( Class cls, const char *name );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Describes the properties declared by a class.

    Declaration

    Swift

    func class_copyPropertyList(_ cls: AnyClass!, _ outCount: UnsafeMutablePointer<UInt32>) -> UnsafeMutablePointer<objc_property_t>

    Objective-C

    objc_property_t * class_copyPropertyList ( Class cls, unsigned int *outCount );

    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 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Adds a new method to a class with a given name and implementation.

    Declaration

    Swift

    func class_addMethod(_ cls: AnyClass!, _ name: Selector, _ imp: IMP, _ types: UnsafePointer<Int8>) -> Bool

    Objective-C

    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

    YEStrue if the method was added successfully, otherwise NOfalse (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.

    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@:");

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a specified instance method for a given class.

    Declaration

    Swift

    func class_getInstanceMethod(_ aClass: AnyClass!, _ aSelector: Selector) -> Method

    Objective-C

    Method class_getInstanceMethod ( Class cls, SEL name );

    Parameters

    aClass

    The class you want to inspect.

    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 does not.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a pointer to the data structure describing a given class method for a given class.

    Declaration

    Swift

    func class_getClassMethod(_ aClass: AnyClass!, _ aSelector: Selector) -> Method

    Objective-C

    Method class_getClassMethod ( Class cls, SEL name );

    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. Pass the selector of the method you want to retrieve.

    Return Value

    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 a class method with the specified selector.

    Discussion

    Note that this function searches superclasses for implementations, whereas class_copyMethodList does not.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Describes the instance methods implemented by a class.

    Declaration

    Swift

    func class_copyMethodList(_ cls: AnyClass!, _ outCount: UnsafeMutablePointer<UInt32>) -> UnsafeMutablePointer<Method>

    Objective-C

    Method * class_copyMethodList ( Class cls, unsigned int *outCount );

    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).

    To get the implementations of methods that may be implemented by superclasses, use class_getInstanceMethod or class_getClassMethod.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Replaces the implementation of a method for a given class.

    Declaration

    Swift

    func class_replaceMethod(_ cls: AnyClass!, _ name: Selector, _ imp: IMP, _ types: UnsafePointer<Int8>) -> IMP

    Objective-C

    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 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 were called. The type encoding specified by types is ignored.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the function pointer that would be called if a particular message were sent to an instance of a class.

    Declaration

    Swift

    func class_getMethodImplementation(_ cls: AnyClass!, _ name: Selector) -> IMP

    Objective-C

    IMP class_getMethodImplementation ( Class cls, SEL name );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the function pointer that would be called if a particular message were sent to an instance of a class.

    Declaration

    Swift

    func class_getMethodImplementation_stret(_ cls: AnyClass!, _ name: Selector) -> IMP

    Objective-C

    IMP class_getMethodImplementation_stret ( Class cls, SEL name );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a Boolean value that indicates whether instances of a class respond to a particular selector.

    Declaration

    Swift

    func class_respondsToSelector(_ cls: AnyClass!, _ sel: Selector) -> Bool

    Objective-C

    BOOL class_respondsToSelector ( Class cls, SEL sel );

    Parameters

    cls

    The class you want to inspect.

    sel

    A selector.

    Return Value

    YEStrue if instances of the class respond to the selector, otherwise NOfalse.

    Discussion

    You should usually use NSObject's respondsToSelector: or instancesRespondToSelector: methods instead of this function.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Adds a protocol to a class.

    Declaration

    Swift

    func class_addProtocol(_ cls: AnyClass!, _ `outCount`: Protocol!) -> Bool

    Objective-C

    BOOL class_addProtocol ( Class cls, Protocol *protocol );

    Parameters

    cls

    The class to modify.

    outCount

    The protocol to add to cls.

    Return Value

    YEStrue if the protocol was added successfully, otherwise NOfalse (for example, the class already conforms to that protocol).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Adds a property to a class.

    Declaration

    Swift

    func class_addProperty(_ cls: AnyClass!, _ name: UnsafePointer<Int8>, _ attributes: UnsafePointer<objc_property_attribute_t>, _ attributeCount: UInt32) -> Bool

    Objective-C

    BOOL class_addProperty ( Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount );

    Parameters

    cls

    The class to modify.

    name

    The name of the property.

    attributes

    An array of property attributes.

    attributeCount

    The number of attributes in attributes.

    Return Value

    YEStrue if the property was added successfully; otherwise NOfalse (for example, this function returns NOfalse if the class already has that property).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Replace a property of a class.

    Declaration

    Swift

    func class_replaceProperty(_ cls: AnyClass!, _ name: UnsafePointer<Int8>, _ attributes: UnsafePointer<objc_property_attribute_t>, _ attributeCount: UInt32)

    Objective-C

    void class_replaceProperty ( Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount );

    Parameters

    cls

    The class to modify.

    name

    The name of the property.

    attributes

    An array of property attributes.

    attributeCount

    The number of attributes in attributes.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Returns a Boolean value that indicates whether a class conforms to a given protocol.

    Declaration

    Swift

    func class_conformsToProtocol(_ cls: AnyClass!, _ `protocol`: Protocol!) -> Bool

    Objective-C

    BOOL class_conformsToProtocol ( Class cls, Protocol *protocol );

    Parameters

    cls

    The class you want to inspect.

    protocol

    A protocol.

    Return Value

    YEStrue if cls conforms to protocol, otherwise NOfalse.

    Discussion

    You should usually use NSObject‘s conformsToProtocol: method instead of this function.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Describes the protocols adopted by a class.

    Declaration

    Swift

    func class_copyProtocolList(_ cls: AnyClass!, _ outCount: UnsafeMutablePointer<UInt32>) -> AutoreleasingUnsafeMutablePointer<Protocol?>

    Objective-C

    Protocol ** class_copyProtocolList ( Class cls, unsigned int *outCount );

    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().

    If cls adopts no protocols, or cls is Nil, returns NULL and *outCount is 0.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the version number of a class definition.

    Declaration

    Swift

    func class_getVersion(_ theClass: AnyClass!) -> Int32

    Objective-C

    int class_getVersion ( Class cls );

    Parameters

    theClass

    A pointer to an Class 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Sets the version number of a class definition.

    Declaration

    Swift

    func class_setVersion(_ theClass: AnyClass!, _ version: Int32)

    Objective-C

    void class_setVersion ( Class cls, int version );

    Parameters

    theClass

    A pointer to an Class 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Used by CoreFoundation's toll-free bridging.

    Declaration

    Objective-C

    Class objc_getFutureClass ( const char *name );

    Special Considerations

    Do not call this function yourself.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Used by CoreFoundation's toll-free bridging.

    Declaration

    Objective-C

    void objc_setFutureClass ( Class cls, const char *name );

    Special Considerations

    Do not call this function yourself.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Creates a new class and metaclass.

    Declaration

    Swift

    func objc_allocateClassPair(_ superclass: AnyClass!, _ name: UnsafePointer<Int8>, _ extraBytes: Int) -> AnyClass!

    Objective-C

    Class objc_allocateClassPair ( Class superclass, const char *name, size_t extraBytes );

    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 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Destroys a class and its associated metaclass.

    Declaration

    Swift

    func objc_disposeClassPair(_ cls: AnyClass!)

    Objective-C

    void objc_disposeClassPair ( Class cls );

    Parameters

    cls

    The class to be destroyed. This class must have been allocated using objc_allocateClassPair.

    Discussion

    Do not call this function if instances of the cls class or any subclass exist.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Registers a class that was allocated using objc_allocateClassPair.

    Declaration

    Swift

    func objc_registerClassPair(_ cls: AnyClass!)

    Objective-C

    void objc_registerClassPair ( Class cls );

    Parameters

    cls

    The class you want to register.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Used by Foundation's Key-Value Observing.

    Declaration

    Swift

    func objc_duplicateClass(_ original: AnyClass!, _ name: UnsafePointer<Int8>, _ extraBytes: Int) -> AnyClass!

    Objective-C

    Class objc_duplicateClass ( Class original, const char *name, size_t extraBytes );

    Special Considerations

    Do not call this function yourself.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Creates an instance of a class, allocating memory for the class in the default malloc memory zone.

    Declaration

    Objective-C

    id class_createInstance ( Class cls, size_t extraBytes );

    Parameters

    cls

    The class that you want 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Creates an instance of a class at the specified location.

    Declaration

    Objective-C

    id objc_constructInstance ( Class cls, void *bytes );

    Parameters

    cls

    The class that you want to allocate an instance of.

    bytes

    The location at which to allocate an instance of the cls class. bytes myst point to at least class_getInstanceSize(cls) bytes of well-aligned, zero-filled memory.

    Return Value

    An instance of the class cls at bytes, if successful; otherwise nil (for example, if cls or bytes are themselves nil).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 3.0 and later.

  • Destroys an instance of a class without freeing memory and removes any of its associated references.

    Declaration

    Objective-C

    void * objc_destructInstance ( id obj );

    Discussion

    This method does nothing if obj is nil.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 3.0 and later.

  • Returns a copy of a given object.

    Declaration

    Objective-C

    id object_copy ( id obj, size_t size );

    Parameters

    obj

    An Objective-C object.

    size

    The size of the object obj.

    Return Value

    A copy of obj.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Frees the memory occupied by a given object.

    Declaration

    Objective-C

    id object_dispose ( id obj );

    Parameters

    obj

    An Objective-C object.

    Return Value

    nil.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Changes the value of an instance variable of a class instance.

    Declaration

    Objective-C

    Ivar object_setInstanceVariable ( id obj, const char *name, void *value );

    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 data structure that defines the type and name of the instance variable specified by name.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Obtains the value of an instance variable of a class instance.

    Declaration

    Objective-C

    Ivar object_getInstanceVariable ( id obj, const char *name, void **outValue );

    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 data structure that defines the type and name of the instance variable specified by name.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Returns a pointer to any extra bytes allocated with a instance given object.

    Declaration

    Objective-C

    void * object_getIndexedIvars ( id obj );

    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 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Reads the value of an instance variable in an object.

    Declaration

    Swift

    func object_getIvar(_ object: AnyObject!, _ ivar: Ivar) -> AnyObject!

    Objective-C

    id object_getIvar ( id obj, Ivar ivar );

    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 if the Ivar for the instance variable is already known.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Sets the value of an instance variable in an object.

    Declaration

    Swift

    func object_setIvar(_ object: AnyObject!, _ ivar: Ivar, _ value: AnyObject!)

    Objective-C

    void object_setIvar ( id obj, Ivar ivar, id value );

    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.

    value

    The new value for the instance variable.

    Discussion

    object_setIvar is faster than object_setInstanceVariable if the Ivar for the instance variable is already known.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the class name of a given object.

    Declaration

    Objective-C

    const char * object_getClassName ( id obj );

    Parameters

    obj

    An Objective-C object.

    Return Value

    The name of the class of which obj is an instance.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Returns the class of an object.

    Declaration

    Swift

    func object_getClass(_ object: AnyObject!) -> AnyClass!

    Objective-C

    Class object_getClass ( id obj );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Sets the class of an object.

    Declaration

    Swift

    func object_setClass(_ object: AnyObject!, _ sel: AnyClass!) -> AnyClass!

    Objective-C

    Class object_setClass ( id obj, Class cls );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Obtains the list of registered class definitions.

    Declaration

    Swift

    func objc_getClassList(_ buffer: AutoreleasingUnsafeMutablePointer<AnyClass?>, _ bufferLen: Int32) -> Int32

    Objective-C

    int objc_getClassList ( Class *buffer, int bufferCount );

    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.

    Listing 1Using 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);
    • }

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Creates and returns a list of pointers to all registered class definitions.

    Declaration

    Swift

    func objc_copyClassList(_ outCount: UnsafeMutablePointer<UInt32>) -> AutoreleasingUnsafeMutablePointer<AnyClass?>

    Objective-C

    Class * objc_copyClassList ( unsigned int *outCount );

    Parameters

    outCount

    An integer pointer used to store the number of classes returned by this function in the list. This parameter may be nil.

    Return Value

    A nil terminated array of classes. You must free the array with free().

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 3.1 and later.

  • Returns the class definition of a specified class.

    Declaration

    Swift

    func objc_lookUpClass(_ name: UnsafePointer<Int8>) -> AnyClass!

    Objective-C

    Class objc_lookUpClass ( const char *name );

    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 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the class definition of a specified class.

    Declaration

    Swift

    func objc_getClass(_ name: UnsafePointer<Int8>) -> AnyObject!

    Objective-C

    Class objc_getClass ( const char *name );

    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 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.

    Special Considerations

    Earlier implementations of this function (prior to OS X v10.0) terminate the program if the class does not exist.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the class definition of a specified class.

    Declaration

    Swift

    func objc_getRequiredClass(_ name: UnsafePointer<Int8>) -> AnyClass!

    Objective-C

    Class objc_getRequiredClass ( const char *name );

    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, 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the metaclass definition of a specified class.

    Declaration

    Swift

    func objc_getMetaClass(_ name: UnsafePointer<Int8>) -> AnyObject!

    Objective-C

    Class objc_getMetaClass ( const char *name );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the name of an instance variable.

    Declaration

    Swift

    func ivar_getName(_ v: Ivar) -> UnsafePointer<Int8>

    Objective-C

    const char * ivar_getName ( Ivar v );

    Return Value

    A C string containing the instance variable's name.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the type string of an instance variable.

    Declaration

    Swift

    func ivar_getTypeEncoding(_ v: Ivar) -> UnsafePointer<Int8>

    Objective-C

    const char * ivar_getTypeEncoding ( Ivar v );

    Return Value

    A C string containing the instance variable's type encoding.

    Discussion

    For possible values, see Objective-C Runtime Programming Guide > Type Encodings.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the offset of an instance variable.

    Declaration

    Swift

    func ivar_getOffset(_ v: Ivar) -> Int

    Objective-C

    ptrdiff_t ivar_getOffset ( Ivar v );

    Discussion

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func objc_setAssociatedObject(_ object: AnyObject!, _ key: UnsafePointer<Void>, _ value: AnyObject!, _ policy: objc_AssociationPolicy)

    Objective-C

    void objc_setAssociatedObject ( id object, const void *key, id value, objc_AssociationPolicy policy );

    Parameters

    object

    The source object for the association.

    key

    The key for the association.

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 3.1 and later.

  • Returns the value associated with a given object for a given key.

    Declaration

    Swift

    func objc_getAssociatedObject(_ object: AnyObject!, _ key: UnsafePointer<Void>) -> AnyObject!

    Objective-C

    id objc_getAssociatedObject ( id object, const void *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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 3.1 and later.

  • Removes all associations for a given object.

    Declaration

    Swift

    func objc_removeAssociatedObjects(_ object: AnyObject!)

    Objective-C

    void objc_removeAssociatedObjects ( id 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 with a nil value to clear an association.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 3.1 and later.

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.

  • Sends a message with a simple return value to an instance of a class.

    Declaration

    Objective-C

    id objc_msgSend ( id self, SEL op, ... );

    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.

    Return Value

    The return value of the method.

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Sends a message with a data-structure return value to an instance of a class.

    Declaration

    Objective-C

    void objc_msgSend_stret ( id self, SEL op, ... );

    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. Pass the selector of the method that handles the message.

    ...

    A variable argument list containing the arguments to the method.

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Sends a message with a simple return value to the superclass of an instance of a class.

    Declaration

    Objective-C

    id objc_msgSendSuper ( struct objc_super *super, SEL op, ... );

    Parameters

    super

    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.

    op

    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.

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Sends a message with a data-structure return value to the superclass of an instance of a class.

    Declaration

    Objective-C

    void objc_msgSendSuper_stret ( struct objc_super *super, SEL op, ... );

    Parameters

    super

    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.

    op

    A pointer of type SEL. Pass the selector of the method.

    ...

    A variable argument list containing the arguments to the method.

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Calls the implementation of a specified method.

    Declaration

    Objective-C

    id method_invoke ( id receiver, Method m, ... );

    Parameters

    receiver

    A pointer to the instance of the class that you want to invoke the method on. This value must not be nil.

    m

    The method whose implementation you want to call.

    ...

    A variable argument list containing the arguments to the method.

    Return Value

    The return value of the method.

    Discussion

    Using this function to call the implementation of a method is faster than calling method_getImplementation and method_getName.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Calls the implementation of a specified method that returns a data-structure.

    Declaration

    Objective-C

    void method_invoke_stret ( id receiver, Method m, ... );

    Parameters

    receiver

    A pointer to the instance of the class that you want to invoke the method on. This value must not be nil.

    m

    The method whose implementation you want to call.

    ...

    A variable argument list containing the arguments to the method.

    Discussion

    Using this function to call the implementation of a method is faster than calling method_getImplementation and method_getName.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Returns the name of a method.

    Declaration

    Swift

    func method_getName(_ method: Method) -> Selector

    Objective-C

    SEL method_getName ( Method m );

    Parameters

    method

    The method to inspect.

    Return Value

    A pointer of type SEL.

    Discussion

    To get the method name as a C string, call sel_getName(method_getName(method)).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the implementation of a method.

    Declaration

    Swift

    func method_getImplementation(_ method: Method) -> IMP

    Objective-C

    IMP method_getImplementation ( Method m );

    Parameters

    method

    The method to inspect.

    Return Value

    A function pointer of type IMP.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a string describing a method's parameter and return types.

    Declaration

    Swift

    func method_getTypeEncoding(_ method: Method) -> UnsafePointer<Int8>

    Objective-C

    const char * method_getTypeEncoding ( Method m );

    Parameters

    method

    The method to inspect.

    Return Value

    A C string. The string may be NULL.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a string describing a method's return type.

    Declaration

    Swift

    func method_copyReturnType(_ method: Method) -> UnsafeMutablePointer<Int8>

    Objective-C

    char * method_copyReturnType ( Method m );

    Parameters

    method

    The method to inspect.

    Return Value

    A C string describing the return type. You must free the string with free().

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a string describing a single parameter type of a method.

    Declaration

    Swift

    func method_copyArgumentType(_ method: Method, _ index: UInt32) -> UnsafeMutablePointer<Int8>

    Objective-C

    char * method_copyArgumentType ( Method m, unsigned int index );

    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().

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns by reference a string describing a method's return type.

    Declaration

    Swift

    func method_getReturnType(_ m: Method, _ dst: UnsafeMutablePointer<Int8>, _ dst_len: Int)

    Objective-C

    void method_getReturnType ( Method m, char *dst, size_t dst_len );

    Discussion

    The method's return type string is copied to dst. dst is filled as if strncpy(dst, parameter_type, dst_len) were called.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the number of arguments accepted by a method.

    Declaration

    Swift

    func method_getNumberOfArguments(_ method: Method) -> UInt32

    Objective-C

    unsigned int method_getNumberOfArguments ( Method m );

    Parameters

    method

    A pointer to a Method data structure. Pass the method in question.

    Return Value

    An integer containing the number of arguments accepted by the given method.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns by reference a string describing a single parameter type of a method.

    Declaration

    Swift

    func method_getArgumentType(_ m: Method, _ index: UInt32, _ dst: UnsafeMutablePointer<Int8>, _ dst_len: Int)

    Objective-C

    void method_getArgumentType ( Method m, unsigned int index, char *dst, size_t dst_len );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a method description structure for a specified method.

    Declaration

    Swift

    func method_getDescription(_ m: Method) -> UnsafeMutablePointer<objc_method_description>

    Objective-C

    struct objc_method_description * method_getDescription ( Method m );

    Parameters

    m

    The method you want to inquire about.

    Return Value

    An objc_method_description structure that describes the method specified by m.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Sets the implementation of a method.

    Declaration

    Swift

    func method_setImplementation(_ m: Method, _ imp: IMP) -> IMP

    Objective-C

    IMP method_setImplementation ( Method m, IMP imp );

    Return Value

    The previous implementation of the method.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Exchanges the implementations of two methods.

    Declaration

    Swift

    func method_exchangeImplementations(_ m1: Method, _ m2: Method)

    Objective-C

    void method_exchangeImplementations ( Method m1, Method m2 );

    Discussion

    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);

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the names of all the loaded Objective-C frameworks and dynamic libraries.

    Declaration

    Swift

    func objc_copyImageNames(_ outCount: UnsafeMutablePointer<UInt32>) -> UnsafeMutablePointer<UnsafePointer<Int8>>

    Objective-C

    const char ** objc_copyImageNames ( unsigned int *outCount );

    Parameters

    outCount

    The number of names in the returned array.

    Return Value

    An array of C strings representing the names of all the loaded Objective-C frameworks and dynamic libraries.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the name of the dynamic library a class originated from.

    Declaration

    Swift

    func class_getImageName(_ cls: AnyClass!) -> UnsafePointer<Int8>

    Objective-C

    const char * class_getImageName ( Class cls );

    Parameters

    cls

    The class you are inquiring about.

    Return Value

    A C string representing the name of the library containing the cls class.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the names of all the classes within a specified library or framework.

    Declaration

    Swift

    func objc_copyClassNamesForImage(_ image: UnsafePointer<Int8>, _ outCount: UnsafeMutablePointer<UInt32>) -> UnsafeMutablePointer<UnsafePointer<Int8>>

    Objective-C

    const char ** objc_copyClassNamesForImage ( const char *image, unsigned int *outCount );

    Parameters

    image

    The library or framework you are inquiring about.

    outCount

    The number of class names in the returned array.

    Return Value

    An array of C strings representing all of the class names within the specified library or framework.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the name of the method specified by a given selector.

    Declaration

    Objective-C

    const char * sel_getName ( SEL sel );

    Parameters

    aSelector

    A pointer of type SEL. Pass the selector whose name you wish to determine.

    Return Value

    A C string indicating the name of the selector.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Objective-C

    SEL sel_registerName ( const char *str );

    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 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Registers a method name with the Objective-C runtime system.

    Declaration

    Objective-C

    SEL sel_getUid ( const char *str );

    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 specifying the selector for the named method.

    Discussion

    The implementation of this method is identical to the implementation of sel_registerName.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 2.0 and later.

  • Returns a Boolean value that indicates whether two selectors are equal.

    Declaration

    Swift

    func sel_isEqual(_ lhs: Selector, _ rhs: Selector) -> Bool

    Objective-C

    BOOL sel_isEqual ( SEL lhs, SEL rhs );

    Parameters

    lhs

    The selector to compare with rhs.

    rhs

    The selector to compare with lhs.

    Return Value

    YEStrue if rhs and rhs are equal, otherwise NOfalse.

    Discussion

    sel_isEqual is equivalent to ==.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a specified protocol.

    Declaration

    Swift

    func objc_getProtocol(_ name: UnsafePointer<Int8>) -> Protocol!

    Objective-C

    Protocol * objc_getProtocol ( const char *name );

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns an array of all the protocols known to the runtime.

    Declaration

    Swift

    func objc_copyProtocolList(_ outCount: UnsafeMutablePointer<UInt32>) -> AutoreleasingUnsafeMutablePointer<Protocol?>

    Objective-C

    Protocol ** objc_copyProtocolList ( unsigned int *outCount );

    Parameters

    outCount

    Upon return, contains the number of protocols in the returned array.

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Creates a new protocol instance.

    Declaration

    Swift

    func objc_allocateProtocol(_ name: UnsafePointer<Int8>) -> Protocol!

    Objective-C

    Protocol * objc_allocateProtocol ( const char *name );

    Parameters

    name

    The name of the protocol you want to create.

    Return Value

    A new protocol instance or nil if a protocol with the same name as name already exists.

    Discussion

    You must register the returned protocol instance with the objc_registerProtocol function before you can use it.

    There is no dispose method associated with this function.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Registers a newly created protocol with the Objective-C runtime.

    Declaration

    Swift

    func objc_registerProtocol(_ proto: Protocol!)

    Objective-C

    void objc_registerProtocol ( Protocol *proto );

    Parameters

    proto

    The protocol you want to register with the Objective-C runtime.

    Discussion

    When you create a new protocol using the objc_allocateProtocol, you then register it with the Objective-C runtime by calling this function. After a protocol is successfully registered, it is immutable and ready to use.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Adds a method to a protocol.

    Declaration

    Swift

    func protocol_addMethodDescription(_ proto: Protocol!, _ name: Selector, _ types: UnsafePointer<Int8>, _ isRequiredMethod: Bool, _ isInstanceMethod: Bool)

    Objective-C

    void protocol_addMethodDescription ( Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod );

    Parameters

    proto

    The protocol you want to add a method to.

    name

    The name of the method you want to add.

    types

    A C string representing the signature of the method you want to add.

    isRequiredMethod

    A Boolean indicating whether the method is a required method of the proto protocol. If YEStrue, the method is a required method; if NOfalse, the method is an optional method.

    isInstanceMethod

    A Boolean indicating whether the method is an instance method. If YEStrue, the method is an instance method; if NOfalse, the method is a class method.

    Discussion

    To add a method to a protocol using this function, the protocol must be under construction. That is, you must add any methods to proto before you register it with the Objective-C runtime (via the objc_registerProtocol function).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Adds a registered protocol to another protocol that is under construction.

    Declaration

    Swift

    func protocol_addProtocol(_ proto: Protocol!, _ addition: Protocol!)

    Objective-C

    void protocol_addProtocol ( Protocol *proto, Protocol *addition );

    Parameters

    proto

    The protocol you want to add the registered protocol to.

    addition

    The registered protocol you want to add to proto.

    Discussion

    The protocol you want to add to (proto) must be under construction—allocated but not yet registered with the Objective-C runtime. The protocol you want to add (addition) must be registered already.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Adds a property to a protocol that is under construction.

    Declaration

    Swift

    func protocol_addProperty(_ proto: Protocol!, _ name: UnsafePointer<Int8>, _ attributes: UnsafePointer<objc_property_attribute_t>, _ attributeCount: UInt32, _ isRequiredProperty: Bool, _ isInstanceProperty: Bool)

    Objective-C

    void protocol_addProperty ( Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty );

    Parameters

    proto

    The protocol you want to add a property to.

    name

    The name of the property you want to add.

    attributes

    An array of property attributes.

    attributeCount

    The number of properties in attributes.

    isRequiredProperty

    A Boolean indicating whether the property’s accessor methods are required methods of the proto protocol. If YEStrue, the property’s accessor methods are required methods; if NOfalse, the property’s accessor methods are optional methods.

    isInstanceProperty

    A Boolean indicating whether the property’s accessor methods are instance methods. If YEStrue, the property’s accessor methods are instance methods. YEStrue is the only value allowed for a property. As a result, if you set this value to NOfalse, the property will not be added to the protocol.

    Discussion

    The protocol you want to add the property to must be under construction—allocated but not yet registered with the Objective-C runtime (via the objc_registerProtocol function).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Returns a the name of a protocol.

    Declaration

    Swift

    func protocol_getName(_ p: Protocol!) -> UnsafePointer<Int8>

    Objective-C

    const char * protocol_getName ( Protocol *p );

    Parameters

    p

    A protocol.

    Return Value

    The name of the protocol p as a C string.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a Boolean value that indicates whether two protocols are equal.

    Declaration

    Swift

    func protocol_isEqual(_ proto: Protocol!, _ other: Protocol!) -> Bool

    Objective-C

    BOOL protocol_isEqual ( Protocol *proto, Protocol *other );

    Parameters

    proto

    A protocol.

    other

    A protocol.

    Return Value

    YEStrue if proto is the same as other, otherwise NOfalse.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns an array of method descriptions of methods meeting a given specification for a given protocol.

    Declaration

    Swift

    func protocol_copyMethodDescriptionList(_ p: Protocol!, _ isRequiredMethod: Bool, _ isInstanceMethod: Bool, _ outCount: UnsafeMutablePointer<UInt32>) -> UnsafeMutablePointer<objc_method_description>

    Objective-C

    struct objc_method_description * protocol_copyMethodDescriptionList ( Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount );

    Parameters

    p

    A protocol.

    isRequiredMethod

    A Boolean value that indicates whether returned methods should be required methods (pass YEStrue to specify required methods).

    isInstanceMethod

    A Boolean value that indicates whether returned methods should be instance methods (pass YEStrue 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a method description structure for a specified method of a given protocol.

    Declaration

    Swift

    func protocol_getMethodDescription(_ p: Protocol!, _ aSel: Selector, _ isRequiredMethod: Bool, _ isInstanceMethod: Bool) -> objc_method_description

    Objective-C

    struct objc_method_description protocol_getMethodDescription ( Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod );

    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.

    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}.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns an array of the properties declared by a protocol.

    Declaration

    Swift

    func protocol_copyPropertyList(_ proto: Protocol!, _ outCount: UnsafeMutablePointer<UInt32>) -> UnsafeMutablePointer<objc_property_t>

    Objective-C

    objc_property_t * protocol_copyPropertyList ( Protocol *proto, unsigned int *outCount );

    Parameters

    proto

    A protocol.

    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().

    If the protocol declares no properties, NULL is returned and *outCount is 0.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the specified property of a given protocol.

    Declaration

    Swift

    func protocol_getProperty(_ proto: Protocol!, _ name: UnsafePointer<Int8>, _ isRequiredProperty: Bool, _ isInstanceProperty: Bool) -> objc_property_t

    Objective-C

    objc_property_t protocol_getProperty ( Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty );

    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 an instance property.

    Return Value

    The property specified by name, isRequiredProperty, and isInstanceProperty for proto, or NULL if none of proto’s properties meets the specification.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns an array of the protocols adopted by a protocol.

    Declaration

    Swift

    func protocol_copyProtocolList(_ proto: Protocol!, _ outCount: UnsafeMutablePointer<UInt32>) -> AutoreleasingUnsafeMutablePointer<Protocol?>

    Objective-C

    Protocol ** protocol_copyProtocolList ( Protocol *proto, unsigned int *outCount );

    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().

    If the protocol declares no properties, NULL is returned and *outCount is 0.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns a Boolean value that indicates whether one protocol conforms to another protocol.

    Declaration

    Swift

    func protocol_conformsToProtocol(_ proto: Protocol!, _ other: Protocol!) -> Bool

    Objective-C

    BOOL protocol_conformsToProtocol ( Protocol *proto, Protocol *other );

    Parameters

    proto

    A protocol.

    other

    A protocol.

    Return Value

    YEStrue if proto conforms to other, otherwise NOfalse.

    Discussion

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the name of a property.

    Declaration

    Swift

    func property_getName(_ property: objc_property_t) -> UnsafePointer<Int8>

    Objective-C

    const char * property_getName ( objc_property_t property );

    Return Value

    A C string containing the property's name.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the attribute string of a property.

    Declaration

    Swift

    func property_getAttributes(_ property: objc_property_t) -> UnsafePointer<Int8>

    Objective-C

    const char * property_getAttributes ( objc_property_t 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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Returns the value of a property attribute given the attribute name.

    Declaration

    Swift

    func property_copyAttributeValue(_ property: objc_property_t, _ attributeName: UnsafePointer<Int8>) -> UnsafeMutablePointer<Int8>

    Objective-C

    char * property_copyAttributeValue ( objc_property_t property, const char *attributeName );

    Parameters

    property

    The property whose value you are interested in.

    attributeName

    A C string representing the name of the attribute.

    Return Value

    The value string of the attributeName attribute, if one exists in property; otherwise, nil. You must free the returned value string with free().

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Returns an array of property attributes for a given property.

    Declaration

    Swift

    func property_copyAttributeList(_ property: objc_property_t, _ outCount: UnsafeMutablePointer<UInt32>) -> UnsafeMutablePointer<objc_property_attribute_t>

    Objective-C

    objc_property_attribute_t * property_copyAttributeList ( objc_property_t property, unsigned int *outCount );

    Parameters

    property

    The property whose attributes you want to copy.

    outCount

    The number of attributes returned in the array.

    Return Value

    An array of property attributes. You must free the array with free().

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Inserted by the compiler when a mutation is detected during a foreach iteration.

    Declaration

    Swift

    func objc_enumerationMutation(_ obj: AnyObject!)

    Objective-C

    void objc_enumerationMutation ( id obj );

    Parameters

    obj

    The object being mutated.

    Discussion

    The compiler inserts this function when it detects that an object is mutated during a foreach iteration. The function is called when a mutation occurs, and the enumeration mutation handler is enacted if it is set up (via the objc_setEnumerationMutationHandler function). If the handler is not set up, a fatal error occurs.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Sets the current mutation handler.

    Declaration

    Swift

    func objc_setEnumerationMutationHandler(_ handler: CFunctionPointer<((AnyObject!) -> Void)>)

    Objective-C

    void objc_setEnumerationMutationHandler ( void (*handler)(id) );

    Parameters

    handler

    A function pointer to the new mutation handler.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 2.0 and later.

  • Creates a pointer to a function that calls the specified block when the method is called.

    Declaration

    Swift

    func imp_implementationWithBlock(_ block: AnyObject!) -> IMP

    Objective-C

    IMP imp_implementationWithBlock ( id block );

    Parameters

    block

    The block that implements this method. The signature of block should be method_return_type ^(id self, self, method_args …). The selector of the method is not available to block. block is copied with Block_copy().

    Return Value

    The IMP that calls block. You must dispose of the returned IMP using the function.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

    See Also

    imp_getBlock

  • Returns the block associated with an IMP that was created using imp_implementationWithBlock.

    Declaration

    Swift

    func imp_getBlock(_ anImp: IMP) -> AnyObject!

    Objective-C

    id imp_getBlock ( IMP anImp );

    Parameters

    anImp

    The IMP that calls this block.

    Return Value

    The block called by anImp.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Disassociates a block from an IMP that was created using imp_implementationWithBlock, and releases the copy of the block that was created.

    Declaration

    Swift

    func imp_removeBlock(_ anImp: IMP) -> Bool

    Objective-C

    BOOL imp_removeBlock ( IMP anImp );

    Parameters

    anImp

    An IMP that was created using the imp_implementationWithBlock function.

    Return Value

    YEStrue if the block was released successfully; otherwise, NOfalse (for example, the function returns NOfalse if the block was not used to create anImp previously).

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.3 and later.

  • Loads the object referenced by a weak pointer and returns it.

    Declaration

    Swift

    func objc_loadWeak(_ location: AutoreleasingUnsafeMutablePointer<AnyObject?>) -> AnyObject!

    Objective-C

    id objc_loadWeak ( id *location );

    Parameters

    location

    The address of the weak pointer.

    Return Value

    The object pointed to by location, or nil if location is nil.

    Discussion

    This function loads the object referenced by a weak pointer and returns it after retaining and autoreleasing the object. As a result, the object stays alive long enough for the caller to use it. This function is typically used anywhere a __weak variable is used in an expression.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 5.0 and later.

  • Stores a new value in a __weak variable.

    Declaration

    Swift

    func objc_storeWeak(_ location: AutoreleasingUnsafeMutablePointer<AnyObject?>, _ obj: AnyObject!) -> AnyObject!

    Objective-C

    id objc_storeWeak ( id *location, id obj );

    Parameters

    location

    The address of the weak pointer.

    obj

    The new object you want the weak pointer to now point to.

    Return Value

    The value stored in location (that is, obj).

    Discussion

    This function is typically used anywhere a __weak variable is the target of an assignment.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 5.0 and later.

Data Types

Class-Definition Data Structures

  • An opaque type that represents an Objective-C class.

    Declaration

    Objective-C

    typedef struct objc_class *Class;

    Availability

    Available in iOS 4.0 and later.

  • An opaque type that represents a method in a class definition.

    Declaration

    Swift

    typealias Method = COpaquePointer

    Objective-C

    typedef struct objc_method *Method;

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.0 and later.

  • An opaque type that represents an instance variable.

    Declaration

    Swift

    typealias Ivar = COpaquePointer

    Objective-C

    typedef struct objc_ivar *Ivar;

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.0 and later.

  • An opaque type that represents a category.

    Declaration

    Swift

    typealias Category = COpaquePointer

    Objective-C

    typedef struct objc_category *Category;

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.0 and later.

  • An opaque type that represents an Objective-C declared property.

    Declaration

    Swift

    typealias objc_property_t = COpaquePointer

    Objective-C

    typedef struct objc_property *objc_property_t;

    Import Statement

    Objective-C

    @import ObjectiveC;

    Swift

    import ObjectiveC

    Availability

    Available in iOS 4.0 and later.

  • A pointer to the start of a method implementation.

    Declaration

    Objective-C

    id (*IMP)(id, SEL, ...)

    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.

    Import Statement

  • Defines an opaque type that represents a method selector.

    Declaration

    Objective-C

    typedef struct objc_selector *SEL;

    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.

    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.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 4.0 and later.

  • Defines an Objective-C method.

    Declaration

    Swift

    struct objc_method_description { var name: Selector var types: UnsafeMutablePointer<Int8> init() init(name name: Selector, types types: UnsafeMutablePointer<Int8>) }

    Objective-C

    struct objc_method_description { SEL name; char *types; };

    Fields

    name

    The name of the method at runtime.

    types

    The types of the method arguments.

    Availability

    Available in iOS 4.0 and later.

  • Performance optimization for method calls. Contains pointers to recently used methods.

    Declaration

    Objective-C

    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 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.

  • Defines a property attribute.

    Declaration

    Objective-C

    typedef struct { const char *name; const char *value; } objc_property_attribute_t;

    Fields

    name

    The name of the attribute.

    value

    The value of the attribute (usually empty).

    Availability

    Available in iOS 4.3 and later.

Instance Data Types

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.

  • id id

    A pointer to an instance of a class.

    Declaration

    Objective-C

    typedef struct objc_object *id;

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 4.0 and later.

  • Represents an instance of a class.

    Declaration

    Objective-C

    struct objc_object { Class isa; };

    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 to create objc_object data structures.

  • Specifies the superclass of an instance.

    Declaration

    Swift

    struct objc_super { var receiver: AnyObject! var super_class: AnyClass! init() init(receiver receiver: AnyObject!, super_class super_class: AnyClass!) }

    Objective-C

    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 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.

    Availability

    Available in iOS 4.0 and later.

Boolean Value

  • Type to represent a Boolean value.

    Declaration

    Objective-C

    typedef signed char BOOL;

    Discussion

    BOOL is explicitly signed so @encode(BOOL) is c rather than C even if -funsigned-char is used.

    For values, see “Boolean Values”.

    Import Statement

    Objective-C

    @import ObjectiveC;

    Availability

    Available in iOS 4.0 and later.

Associative References

Constants

  • These macros define convenient constants to represent Boolean values.

    Declaration

    Objective-C

    #define YES (BOOL)1 #define NO (BOOL)0

    Constants

    • YES

      YES

      Defines YES as 1.

      Available in iOS 2.0 and later.

    • NO

      NO

      Defines NO as 0.

      Available in iOS 2.0 and later.

  • These macros define null values for classes and instances.

    Declaration

    Objective-C

    #define nil __DARWIN_NULL #define Nil __DARWIN_NULL

    Constants

    • nil

      nil

      Defines the id of a null instance.

      Available in iOS 4.0 and later.

    • Nil

      Nil

      Defines the id of a null class.

      Available in iOS 4.0 and later.

  • This macro indicates whether dispatch functions must be cast to an appropriate function pointer type.

    Declaration

    Swift

    var OBJC_OLD_DISPATCH_PROTOTYPES: Int32 { get }

    Objective-C

    #define OBJC_OLD_DISPATCH_PROTOTYPES 1

    Constants

    • OBJC_OLD_DISPATCH_PROTOTYPES

      OBJC_OLD_DISPATCH_PROTOTYPES

      OBJC_OLD_DISPATCH_PROTOTYPES == 0 enforces the rule that the dispatch functions must be cast to an appropriate function pointer type.

      Available in iOS 6.0 and later.

  • This macro annotates a class as being an Objective-C root class.

    Declaration

    Objective-C

    #define OBJC_ROOT_CLASS

    Constants

    • OBJC_ROOT_CLASS

      OBJC_ROOT_CLASS

      If you define an Objective-C root class, you receive a compiler error indicating that the class is defined without specifying a base class. You can avoid this compiler error by preceding the definition of the root class (that is, before the @interface directive) with OBJC_ROOT_CLASS.

      Available in iOS 7.0 and later.

  • This macro indicates that the values stored in certain local variables should not be aggressively released by the compiler during optimization.

    Declaration

    Objective-C

    #define NS_VALID_UNTIL_END_OF_SCOPE

    Constants

    • NS_VALID_UNTIL_END_OF_SCOPE

      NS_VALID_UNTIL_END_OF_SCOPE

      Marks local variables of type id or pointer-to-ObjC-object-type so that values stored into those local variable are not aggressively released by the compiler during optimization. Instead, the values are held until either the variable is assigned to again, or the end of the scope of the local variable (such as in a compound statement or a method definition).

      Available in iOS 6.0 and later.

  • Policies related to associative references.

    Declaration

    Swift

    var OBJC_ASSOCIATION_ASSIGN: Int { get } var OBJC_ASSOCIATION_RETAIN_NONATOMIC: Int { get } var OBJC_ASSOCIATION_COPY_NONATOMIC: Int { get } var OBJC_ASSOCIATION_RETAIN: Int { get } var OBJC_ASSOCIATION_COPY: Int { get }

    Objective-C

    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

      OBJC_ASSOCIATION_ASSIGN

      Specifies a weak reference to the associated object.

      Available in iOS 4.0 and later.

    • OBJC_ASSOCIATION_RETAIN_NONATOMIC

      OBJC_ASSOCIATION_RETAIN_NONATOMIC

      Specifies a strong reference to the associated object, and that the association is not made atomically.

      Available in iOS 4.0 and later.

    • OBJC_ASSOCIATION_COPY_NONATOMIC

      OBJC_ASSOCIATION_COPY_NONATOMIC

      Specifies that the associated object is copied, and that the association is not made atomically.

      Available in iOS 4.0 and later.

    • OBJC_ASSOCIATION_RETAIN

      OBJC_ASSOCIATION_RETAIN

      Specifies a strong reference to the associated object, and that the association is made atomically.

      Available in iOS 4.0 and later.

    • OBJC_ASSOCIATION_COPY

      OBJC_ASSOCIATION_COPY

      Specifies that the associated object is copied, and that the association is made atomically.

      Available in iOS 4.0 and later.