Adds a new method to a class with a given name and implementation.
新しいメソッドをある与えられた名前と実装をもつクラスに加えます。
Availability
iOS 2.0+
iPadOS 2.0+
macOS 10.5+
tvOS 9.0+
watchOS 2.0+
Technology
Objective-C Runtime
Objective-Cランタイム
Declaration
宣言
BOOLclass_addMethod(Class cls, SEL name, IMP imp, constchar *types);
Parameters
パラメータ
cls
The class to which to add a method.
name
A selector that specifies the name of the method being added.
imp
A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
types
An array of characters that describe the types of the arguments to the method. For possible values, see Objective-C Runtime Programming Guide > Type Encodings. Since the function must take at least two arguments—self and _cmd, the second and third characters must be “@:” (the first character is the return type).
Return Value
戻り値
YES if the method was added successfully, otherwise NO (for example, the class already contains a method implementation with that name).
Discussion
解説
class_addMethod will add an override of a superclass's implementation, but will not replace an existing implementation in this class. To change an existing implementation, use method_setImplementation.
An Objective-C method is simply a C function that take at least two arguments—self and _cmd. For example, given the following function:
you can dynamically add it to a class as a method (called resolveThisMethodDynamically) like this:
Returns the function pointer that would be called if a particular message were sent to an instance of a class.
関数ポインタを返します、それはある特定のメッセージがあるクラスのあるインスタンスに送られたならば呼び出されるでしょう。
Returns the function pointer that would be called if a particular message were sent to an instance of a class.
関数ポインタを返します、それはある特定のメッセージがあるクラスのあるインスタンスに送られたならば呼び出されるでしょう。
Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
あるブール値を返します、それはあるクラスのインスタンスそれらがある特定のセレクタに応答するかどうかを指し示します。