2017-04-19 40 views
2

使用class_addMethod代码:runtime - 在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); 

回答

2

types参数描述为在所描述的参数和返回类型 方法的 class_addMethod

字符数组描述该类型的参数方法。有关可能的值,请参见Objective-C Runtime Programming Guide > Type Encodings。由于函数必须至少带有两个参数:self_cmd,第二个和第三个字符必须是“@:”(第一个字符是返回类型)。

"@@:"描述了用返回的方法的对象 (类型的编码@,你的情况:UIView *),并从固定的(隐藏的)参数self(对于对象类型的编码@)和_cmd相距不带参数(型对于选择器编码:)。

+0

如果'SEL'是'@selector(viewWithObject:)','types'将是“@@:@”? – tomfriwel

+0

@tom:是的,如果方法将一个对象作为参数并返回一个对象。 –