understanding and application of @selector in iOS development
What is the @selector:
1, a type of SEL
2, on behalf of the message you want to send (method), and a bit like the string, you can turn.: nsselectorfromstring ()/nsselectorfromstring ()
3, something that can be understood as a function pointer--is the thing that allows objective-c to invoke methods dynamically.--object-c dynamic post-binding technique a function pointer that can be accessed through strings
4, is actually the message response function---Select a message response function address to your action
5, @selector (function_name) to obtain a function of the ID OBJC_MSGXXX series function is global
Performselector is NSObject member method, Ms effect is almost
Objective-c Dynamic Post-binding technology: A function pointer that can be accessed through a string
typedef obj_handler * SEL;
questions about the Objc_msgsend & Performselector series functions. 1. Objc_msgsend:
The book says this function is the OC compiler at compile time, encountered similar to [object Foo] When writing, will be the corresponding OC syntax into C's objc_msgsend function. and a corresponding objc_msgsendsuper.
2. About Performselector Series:
Performselector, Performselector:withdelay:, PerformSelector:withObj:withDelay, XXX ...
In addition to the first, each of the following can specify a delay time, and then put the selector in the current thread's runloop to wait for the call. From the method name, feel performselector = = Performselector:withdelay series, delay is 0 of the case.
My question is:
1, Objc_msgsend This function is directly from [object Foo] turned around. In other words, the middle will not turn into a performselector bar. Is the objc_msgsend synchronized?
2, Performselector:withdelay: This is asynchronous, put into the runloop, if Performselector and performselector:withdelay (delay 0 o'clock) the same words, Is that performselector asynchronous, too? You want to go into runloop, too?
Answer:
1, yes, do not performselector, direct objc_msgsend. In fact, this is the OC compile time to turn the good, that is, OC compile the object call to turn into a function called
2, yes, delay for 0 also into the Runloop, the advantage is that you can not stop the current call Performselector:withdelay: The execution of the method
How to confirm the answer above is no problem.
Quite simply, write a few test code, then interrupt the point run and look at the call stack at run time.
The best way to watch a program is always the debugger ~
3, the delegate attribute uses the assign reason.
Circular Reference
All reference counting systems are subject to cyclic application. For example, the following reference relationship:
Object A is created and referenced to object B.
Object B is created and referenced to object C.
Object C is created and referenced to object B.
The reference count for B and C is 2 and 1, respectively. When a no longer uses B, call release releases the ownership of B, because C also references B, so B's reference count is 1,b and will not be released. 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, you must interrupt the circular reference and maintain the reference relationship through other rules. For example, our common delegate tend to be the attributes of the Assign method rather than the Retain method, and the assignment does not increase the reference count to prevent unnecessary circular references at both ends of the delegation. If a Uitableviewcontroller object a takes ownership of UITableView object B through retain, the delegate of this UITableView object B is a, if the delegate is retain way, There's basically no chance of releasing these two objects. You should also pay attention to this when you design and use the delegate mode.
Memory leaks due to circular references are also instrument, so be especially careful.
4, the delegate attribute uses the assign reason.
There are also usages that allow the system to have ownership of objects. Like NSObject's performSelector:withObject:afterDelay. If necessary, the call that needs to be displayed is cancelPreviousPerformRequestsWithTarget:selector:object:, otherwise it is possible to generate a memory leak.
In iphone development, dynamically invoke 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 Myspecialclass in your program, the following is an error, and the above writing simply returns an empty object.
Therefore, in some cases, you can use Nsclassfromstring to initialize the class you are unsure about.
For example, in the iphone, Nstask may have this situation, so when you need to use Nstask, it is best to use:
[[Nsclassfromstring (@ "Nstask") ...]]
Instead of directly using [Nstask ...] This kind of wording.
the benefits of nsclassfromstring are:
1 weaken the connection, and therefore does not link the no framework to the program.
2 does not require import because the class is dynamically loaded and can be loaded as long as it exists.
@selector
What @selector is.
11 Types of SEL
2 represents the message you want to send (method), a bit like the string, you can also switch.: Nsselectorfromstring ()/nsselectorfromstring ()
3 can be understood as something like a function pointer--the thing that allows objective-c to invoke methods dynamically.--object-c dynamic post-binding technique a function pointer that can be accessed through a string
4 is the message response function---Select a function address of a message response to your action
5@selector (function_name) that gets the ID of a function
OBJC_MSGXXX series functions are global
Performselector is NSObject member method, Ms effect is almost
about Objc_msgsend & perform Problems with selector series functions. 1. Objc_msgsend: The book says this function is the OC compiler at compile time, encountered similar to [object Foo] When writing, will be the corresponding OC syntax into C's objc_msgsend function. There is also the corresponding Objc_msgsendsuper 2. About Performselector Series: Performselector, Performselector:withdelay:, PerformSelector:withObj:withDelay, XXX ... In addition to the first, the following can specify a delay time, and then put the selector in the current thread's runloop to wait for the call. From the method name, feel performselector = = Performselector:withdelay series, delay is 0 of the case. My question is: 1. Objc_msgsend This function is transferred directly from [object Foo]. In other words, the middle will not turn into a performselector bar. Is the objc_msgsend synchronized? 2. Performselector:withdelay: This is asynchronous, put in Runloop, if Performselector and Performselector:withdelay (delay is 0 o'clock), Is that performselector asynchronous, too? You want to go into runloop, too? Just beginning to see cocoa, there are a lot of 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 time to turn the good, that is, OC compile the object call to turn into a function called 2, yes, delay for 0 also into the Runloop, the advantage is that you can not stop the current call Performselector:withdelay: The execution of the method
How to confirm the answer above is no problem. Quite simply, write a few test code, then interrupt the point run and look at the call stack at run time. The best way to watch a program is always the debugger ~
|
|
Respondstoselector
3, the delegate attribute uses the assign reason.
Circular Reference
All reference counting systems are subject to cyclic application. For example, the following reference relationship:
Object A is created and referenced to object B.
Object B is created and referenced to object C.
Object C is created and referenced to object B.
The reference count for B and C is 2 and 1, respectively. When a no longer uses B, call release releases the ownership of B, because C also references B, so B's reference count is 1,b and will not be released. 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, you must interrupt the circular reference and maintain the reference relationship through other rules. For example, our common delegate tend to be the attributes of the Assign method rather than the Retain method, and the assignment does not increase the reference count to prevent unnecessary circular references at both ends of the delegation. If a Uitableviewcontroller object a takes ownership of UITableView object B through retain, the delegate of this UITableView object B is a, if the delegate is retain way, There's basically no chance of releasing these two objects. You should also pay attention to this when you design and use the delegate mode.
Memory leaks due to circular references are also instrument, so be especially careful.
4, the delegate attribute uses the assign reason.
There are also usages that allow the system to have ownership of objects. Like NSObject's performSelector:withObject:afterDelay. If necessary, the call that needs to be displayed is cancelPreviousPerformRequestsWithTarget:selector:object:, otherwise it is possible to generate a memory leak.
In iphone development, dynamically invoke 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 Myspecialclass in your program, the following is an error, and the above writing simply returns an empty object.
Therefore, in some cases, you can use Nsclassfromstring to initialize the class you are unsure about.
For example, in the iphone, Nstask may have this situation, so when you need to use Nstask, it is best to use:
[[Nsclassfromstring (@ "Nstask") ...]]
Instead of directly using [Nstask ...] This kind of wording.
the benefits of nsclassfromstring are:
1 weaken the connection, and therefore does not link the no framework to the program.
2 does not require import because the class is dynamically loaded and can be loaded as long as it exists.
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])];
}
}