OGeek|极客世界-中国程序员成长平台

标题: ios - 运行时 - 这个 "@@:"在 class_addMethod 中意味着什么? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 01:08
标题: ios - 运行时 - 这个 "@@:"在 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);



Best Answer-推荐答案


types 参数描述了参数和返回类型 方法如中所述 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).

"@@:" 描述了一个返回对象的方法 (类型编码 @,在您的情况下:UIView *)并且除了固定(隐藏)参数 self(类型编码 @ 用于对象)和 _cmd(类型编码 : 用于选择器)。

关于ios - 运行时 - 这个 "@@:"在 class_addMethod 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492021/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4