Function

class_addMethod(_:_:_:_:)

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

Declaration

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

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

true if the method was added successfully, otherwise false (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@:");

See Also

Working with Classes

func class_getSuperclass(AnyClass?) -> AnyClass?

Returns the superclass of a class.

func class_setSuperclass(AnyClass, AnyClass) -> AnyClass

Sets the superclass of a given class.

Deprecated
func class_isMetaClass(AnyClass?) -> Bool

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

func class_getInstanceSize(AnyClass?) -> Int

Returns the size of instances of a class.

func class_getInstanceVariable(AnyClass?, UnsafePointer<Int8>) -> Ivar?

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

func class_getClassVariable(AnyClass?, UnsafePointer<Int8>) -> Ivar?

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

func class_getIvarLayout(AnyClass?) -> UnsafePointer<UInt8>?

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

func class_setIvarLayout(AnyClass?, UnsafePointer<UInt8>?)

Sets the Ivar layout for a given class.

func class_getWeakIvarLayout(AnyClass?) -> UnsafePointer<UInt8>?

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

func class_setWeakIvarLayout(AnyClass?, UnsafePointer<UInt8>?)

Sets the layout for weak Ivars for a given class.

func class_getProperty(AnyClass?, UnsafePointer<Int8>) -> objc_property_t?

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

func class_getInstanceMethod(AnyClass?, Selector) -> Method?

Returns a specified instance method for a given class.

func class_getClassMethod(AnyClass?, Selector) -> Method?

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

func class_replaceMethod(AnyClass?, Selector, IMP, UnsafePointer<Int8>?) -> IMP?

Replaces the implementation of a method for a given class.

func class_getMethodImplementation(AnyClass?, Selector) -> IMP?

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

func class_getMethodImplementation_stret(AnyClass?, Selector) -> IMP?

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

func class_respondsToSelector(AnyClass?, Selector) -> Bool

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

func class_conformsToProtocol(AnyClass?, Protocol?) -> Bool

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

func class_getVersion(AnyClass?) -> Int32

Returns the version number of a class definition.

func class_setVersion(AnyClass?, Int32)

Sets the version number of a class definition.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software