What is @selector?
11 Types of SEL
2 represents the message (method) you want to send, it's a bit like a string, or you can turn it around.: Nsselectorfromstring ()/nsselectorfromstring ()
3 something that can be understood as a function pointer--a thing that allows objective-c to invoke methods dynamically.--is the object-c dynamic post-binding technique a function pointer that can be accessed through a string
4 is actually the message response function---Select a function address for the message response to your action
[Email protected] (function_name) that gets the ID of a function
The OBJC_MSGXXX series functions are global
Performselector is NSObject member method, Ms effect is almost
about Objc_msgsend & ; The problem with Performselector series functions. 1. Objc_msgsend: The book says that this function is the OC compiler when compiling, when encountering a similar [object Foo] notation, the corresponding OC syntax will be converted to C's Objc_msgsend function. There is also a corresponding objc_msgsendsuper 2. About Performselector Series: Performselector, Performselector:withdelay:, PerformSelector:withObj:withDelay, XXX ... In addition to the first one, the next few can specify a delay time, and then put selector in the current thread's runloop to wait for the call. From the method name, the feeling performselector = = = Performselector:withdelay Series, delay is 0 case. My question is: 1. Objc_msgsend This function is transferred directly from [object Foo]? In other words, the middle does not turn into Performselector, right? Is the objc_msgsend synchronized? 2. Performselector:withdelay: This is asynchronous, put into the runloop, if Performselector and Performselector:withdelay (delay is 0 o'clock), Is that performselector asynchronous, too? Do you want to go into the runloop? Just started to see cocoa, there are many do not understand. |
|
| Tianya |
2011-05-11 08:13 |
For: 1, yes, do not performselector, direct objc_msgsend. In fact, this is the OC compile when the good, that is, OC compile time to call the object called function 2, yes, the delay of 0 also into the Runloop, the advantage of doing so can not stop the current call Performselector:withdelay: The execution of the method
How to confirm the above answer is no problem? Very simple, write a few test code, then break the point of running, look at the call stack at runtime. The best way to watch the program run is always the debugger ~ |
|
Respondstoselector
3. The delegate attribute uses the assign reason.
Circular references
All reference counting systems have problems with cyclic applications. For example, the following reference relationship:
Object A was created and referenced to object B.
Object B was created and referenced to object C.
Object C was created and referenced to object B.
At this time, the reference counts for B and C are 2 and 1, respectively. When a no longer uses B, the call release releases the ownership of B, because C also references B, so the reference count for B is 1,b and will not be freed. B is not released, the reference count of C is 1,c and will not be released. From then on, B and C remain in memory forever.
In this case, the circular reference must be interrupted and the reference relationship maintained through other rules. For example, our common delegate is often a property of the Assign mode rather than the Retain method, and the assignment does not increase the reference count to prevent unnecessary circular references on both sides of the delegation. If a Uitableviewcontroller object a acquires ownership of UITableView object B through retain, the delegate of this UITableView object B is a, and if this delegate is retain mode, There's basically no chance of releasing these two objects. You should also be aware of this when designing your delegate model.
Because the memory leaks generated by circular references are instrument, you should be very careful.
4. The delegate attribute uses the assign reason.
There are also some usages that make the system ownership of the object. Like NSObject's performSelector:withObject:afterDelay. If necessary, you need to display the call CancelPreviousPerformRequestsWithTarget:selector:object:, otherwise there may be a memory leak.
In iphone development, dynamic invocation of classes and methods:
Nsclassfromstring
Nsselectorfromstring
In normal terms,
ID MYOBJ = [[Nsclassfromstring (@ "Myspecialclass") alloc] init];
And
ID MYOBJ = [[Myspecialclass alloc] init];
is the same. However, if you do not have the Myspecialclass class in your program, the following is an error, and the above notation simply returns an empty object.
Therefore, in some cases, you can use Nsclassfromstring to initialize classes that you are unsure about.
For example, in the iphone, Nstask may be the case, so when you need to use Nstask, it's best to use:
[[Nsclassfromstring (@ "Nstask") ...]
Instead of directly using [Nstask ...] This notation.
The benefits of nsclassfromstring are:
1 weakens the connection, and therefore does not link the absence of the framework to the program.
2 Import is not required because the class is dynamically loaded and can be loaded as long as it exists.
[CPP]View Plaincopy
- for (int c=0; c<[classnames count]; C + +) {
- NSString *classname=[classnames OBJECTATINDEX:C];
- ID class=[[nsclassfromstring (className) alloc] init];
- for (int i=0; i<[params count]; i++) {
- [Class performselector:nsselectorfromstring ([NSString stringwithformat:@"seta%i", I])];
- }
- }
- Selector in Object C
It functions as a function pointer, and now most of the usage I see is using @selector when calling certain functions to pass a function pointer parameter. It looks for the function followed by selector in the current class, returning a value of the SEL type.
The execution of the SEL variable. Execute using the Performselecor method.
[Object Performselector:sel variable withobject: Parameter 1 withobject: Parameter 2];
When calling Respondstoselector: @selector, this method requires ":" Only if there is a parameter in the approach, and if the method does not require a parameter, it is not required to add the colon. Otherwise, the compilation will not error, only the value returned by the execution is incorrect. Of course, if the method has more than one parameter, multiple colons are required, the parameter name needs to be taken with the name of the parameter.
Such as: There are the following methods:
-(nsstring*) toxmlstring;
This call resembles the following:
[Self respondstoselector: @selector (toxmlstring)]
If the Toxmlstring method is defined as:
-(nsstring*) toxmlstring: (nsstring*) prefix;
Then the call must be preceded by a colon, such as: [Self respondstoselector: @selector (toxmlstring:)]
-(BOOL) iskindofclass:classobj is used to determine whether an instance of a class or its subclasses
-(BOOL) Ismemberofclass:classobj used to determine whether an instance of a class
-(BOOL) Respondstoselector:selector is used to determine whether there is a method named after a name (encapsulated in a selector object passed)
The + (BOOL) Instancesrespondtoselector:selector is used to determine whether an instance has a method named with a name. The difference between this and the above is that the previous method can be used on instances and classes, and this method can only be used on classes.
-(ID) performselector:selector Execute a method
[Objective-c]sel type, @selector selector
1 ID cattle[3];
2 SEL say;
3 SEL Skins;
where ID cattle[3] defines an array for storing cattle or bull objects. This line of code estimates everyone is very familiar with, the author will not repeat. Traditional arrays like this do not fully meet our needs, when we need to do such as append, delete and other operations, it will be very inconvenient. In the following chapters, I will introduce you to the alternative solution Nsarray for traditional arrays.
The second and third lines of the previous code are the focus of this section, which is the SEL type. Objective-c at compile time, it generates a unique ID that distinguishes the method based on the name of the method (including the parameter sequence), which is the SEL type. We need to note that as long as the names of the methods (including the parameter sequences) are the same, their IDs are the same. That is, whether it is a superclass or a subclass, regardless of whether there is a relationship between the superclass and the subclass, the ID is the same as long as the name is the same. In addition to the function name and ID, the compiler will of course compile the method into the code that the machine can execute, so that in a compiled class, a table is generated as shown in the method (this construction belongs to the author to speculate, not officially confirmed, so figure 5-2 is for reference only, we can temporarily consider this).
Figure 5-2, Table of methods
Notice that there is a colon behind the setskincolor because it is a parameter. Because of the existence of such a table, so in the execution of the program, we can easily through the name of the method, get to the ID of the method is what we call the SEL, and vice versa. Here's how to use it:
1 SEL Variable name = @selector (method name);
2 SEL variable name = nsselectorfromstring (string of method name);
3 NSString * Variable name = nsstringfromselector (sel parameter);
The 1th line is the name of the method that is written directly in the program, the 2nd line is the string that writes the method name, and the 3rd line is the name of the method obtained by the SEL variable. Once we have the SEL variable, we can send a message to an object using the following call:
[Object Performselector:sel variable withobject: Parameter 1 withobject: Parameter 2];
Such a mechanism greatly increases the flexibility of our program, we can pass the SEL parameter to a method, let this method execute a method dynamically, we can also use the configuration file to specify the method to be executed, After the program reads the configuration file, it translates the string of the method into the SEL variable and sends the message to the appropriate object.
From an efficient point of view, execution is not done by means of the name of the method, but by the method ID, which is an integer to find the method, because the integer lookup and matching is much faster than the string, so this can improve the efficiency of execution to some extent.
@selector Explanation (Go) (understanding)