tomfriwel
tomfriwel

Reputation: 2635

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

Use class_addMethod Code:

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

What dose the parameter "@@:" mean in this method?

Doc:

/** 
 * 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);

Upvotes: 4

Views: 1742

Answers (2)

Nguyễn Nghĩa
Nguyễn Nghĩa

Reputation: 11

You know the method in Objective has the below signature

(return type)(function_name)(id self, SEL cmd, ...)

@@: mean The first character standing for the return type. @ mean object type (class type) The second self parameter The third is SEL This function doesn't have the parameter therefore end of with :

For example: "v@:i@"

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

mean

  1. v: is the return type with void type @: is self and SEL i is an integer argument @ is an object type argument

You can check it out the Apple document to know futher. https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100

Upvotes: 1

Martin R
Martin R

Reputation: 539705

The types parameter describes the arguments and return type of the method as described in 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).

"@@:" describes a method with returns an object (type encoding @, in your case: UIView *) and takes no arguments apart from the fixed (hidden) arguments self (type encoding @ for object) and _cmd (type encoding : for selector).

Upvotes: 6

Related Questions