运行时 - 这个“@@:”在 class_addMethod 中是什么意思?

runtime - What does this "@@:" mean in class_addMethod?

使用class_addMethod代码:

class_addMethod(newClass, @selector(inputAccessoryView), accessoryImp, "@@:");

这个方法中参数“@@:”是什么意思?

文档:

/** 
 * Adds a new method to a class with a given name and implementation.
 * 
 * @param cls The class to which to add a method.
 * @param name A selector that specifies the name of the method being added.
 * @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
 * @param types An array of characters that describe the types of the arguments to the method. 
 * 
 * @return YES if the method was added successfully, otherwise NO 
 *  (for example, the class already contains a method implementation with that name).
 *
 * @note 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.
 */
OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, 
                             const char *types) 
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);

types参数描述参数和return类型 方法如中所述 class_addMethod:

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 描述了一个对象的方法 (类型编码 @,在您的情况下:UIView *)并且除了固定(隐藏)参数 self(对象的类型编码 @)和 _cmd(选择器的类型编码 :)。

你知道 Objective 中的方法具有以下签名

(return 类型)(function_name)(id self, SEL cmd, ...)

@@: 意思是 第一个字符代表 return 类型。 @平均对象类型(class类型) 第二个self参数 第三个是SEL 此函数没有参数,因此以 :

结尾

例如: "v@:i@"

-(void)functionName:(int)arg arg2:(id)arg2;

平均值

  1. v: 是return类型,类型为void @: 是自我和 SEL 我是一个整数参数 @ 是对象类型参数

你可以查看苹果文档了解更多。 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100