Adds a new method to a class with a given name and implementation.
- iOS 2.0+
- macOS 10.5+
- tvOS 9.0+
- watchOS 2.0+
The class to which to add a method.
A selector that specifies the name of the method being added.
A function which is the implementation of the new method. The function must take at least two arguments—
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—
_cmd, the second and third characters must be “
@:” (the first character is the return type).
true if the method was added successfully, otherwise
false (for example, the class already contains a method implementation with that name).
class_add 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
An Objective-C method is simply a C function that take at least two arguments—
_cmd. For example, given the following function:
you can dynamically add it to a class as a method (called
resolve) like this: