function pointers for @selector () and C + + in Objective-c

Source: Internet
Author: User

First look at the code used in Tomcat:

    //then start the animation//Put the picture in the Animationimages, accept the array parametersSelf.tom.animationImages =Arrayimage; //set the time interval, 81 graphs, the picture is a little longer than the time, otherwise shortSelf.tom.animationDuration = Arrayimage.count *0.074; //set number of repetitionsSelf.tom.animationRepeatCount =1; //Start Animation[Self.tom startanimating]; //end animation, should wait and then empty[Self.tom performselector: @selector (setanimationimages:) Withobject:nil afterDelay:self.tom.animationDuration];

The last line of code, the process of delaying the loading of the set method. The function of @selector (XXXX) is to find a method named XXXX. That means to call the B method of the A object, and [a b]; The returned type is SEL, and if the argument type is SEL, the value returned by @selector (XXXX) is accepted when the method is described .

Memories:

SEL data type in OBJECTIVE-C syntax fast over (5)

The list of methods for each class is stored in the class object, each method has a corresponding SEL type of data, according to the data of an SEL type can find the address of the method, and then call the method. Definition of SEL type:

typedef struct OBJC_SELECTOR *sel;

The SEL is actually a wrapper around the method, wrapping the method into an SEL type of data to find the corresponding method address. Find the method address to invoke the method, in fact, the message sent is the SEL.

Specifically what this objc_selector structure is, depending on the use of GNU or Apple's runtime, the SEL in Mac OS x is actually mapped to a C string, which can be thought of as the name of the method, and it does not point to a concrete method implementation (the IMP type is).

For all classes, as long as the method name is the same, the resulting selector are the same.

See official documents;

-(ID) Performselector: (SEL) Aselector

Sends the specified message to the receiver, and the pie returns the result of the execution, that is, perform (which itself has the meaning of performing the execution) Selector:xxx, is the method that executes the name XXX method, but is executed indirectly, through the SEL Parameter (using @selector (the name of the method) to return the SEL type), locate the method.

See here, @selector (MethodName) is very similar to the concept of a C language function pointer.

In C, the function name can be assigned directly to a functional pointer, and the OC class cannot directly apply a function pointer, which can only be done with a @selector syntax. Its return result is an SEL type. The type essence is the number of the class method (the function address).

recall function Pointer :

The function also has an address, the pointer to the function points to (saves) the address at the beginning of the function code, declares a function pointer, must first declare the function type it points to (return type and parameter type), declares a pointer to the function, the function pointer can be the parameter of another function, and tells the second function which function to use.

    void ToUpper (Char *); // declaring a function first     ...     void (*p) (Char *); // pointer p is a pointer to the function ToUpper

That is: (*p) is also a function, char* is its parameter, void is the return type, the expression (*p) instead of the function name. Because the precedence of the * operator is lower than (), it must be bracketed-(*p), and the meaning of omitting the parentheses is changed:

void  * p (char *); // This means that P is a function that returns a null pointer type

If you declare a function pointer, you can use the function name to represent the address of the function, as well as the pointer. the most common use of function pointers is as a function parameter. and only point to the function is the same type, you can use the function pointer as a function parameter, you can also use the function name as a parameter (function name as an address), cannot have a function array! But you can have an array of function pointers

look at OC again, Gets the selector value.

// object-c Selector @interface Method-(int) Add: (int) Val; @end  // equivalent to defining a method pointer fun = @selector (add:);

Note

You can use a string to find a method

SEL Variable name = nsselectorfromstring (string of method name);

Can run with SEL variable to detect the method name string
NSString * Variable name = nsstringfromselector (sel parameter);

When the corresponding value is obtained, the SEL value is treated as if the function pointer is executed. The execution of the SEL variable is performed using the Performselecor object method.

[Object Performselector:sel variable withobject: Parameter 1 withobject: Parameter 2];

Applications for Selector

Is the thing that allows Objective-c to invoke the method dynamically.

Is Object-c's dynamic post-binding technology

Functions can be accessed through strings

The selector essence is the same as the callback function of C. It is mainly used for loosely coupled communication between two objects. This method basically constructs the communication between the objects and the control between the cocoa libraries.

@selector () in objective-c and function pointers for C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.