@ Selector

Source: Internet
Author: User

@ Selector?

1. Sel
2 indicates the message (method) You want to sendStringIt is a bit like, and can also be converted to each other.: nsselectorfromstring ()/nsselectorfromstring ()

3. It can be understood as something similar to a function pointer-something that allows objective-C to dynamically call a method-it is a function pointer that object-C's dynamic post-binding technology can access through strings.
4 is actually the message response function-select a function address for the message response to your action
5 @ selector (function_name) is used to obtain the ID of a function.

The objc_msgxxx series functions are global functions.
Performselector is a nsobject member method, which has the same effect as Ms.

About the objc_msgsend & performselector series functions.

1. Objc_msgsend:
The book says that when the OC compiler compiles this function, it will convert the corresponding OC syntax into the objc_msgsend function of C when it encounters a writing similar to [object Foo. There are also corresponding objc_msgsendsuper

2. About the performselector series:
Performselector, performselector: withdelay:, performselector: withobj: withdelay, xxx...
In addition to the first one, you can specify a delay time for the next few threads, and then put the selector in the runloop of the current thread to wait for the call. From the method name, it seems that the delay value is 0 in the withdelay series.

My problem is:
1. Is the function objc_msgsend directly transferred from [object Foo? That is to say, it will not be converted into an performselector in the middle, right? Is objc_msgsend synchronized?
2. Performselector: withdelay: This is asynchronous. Put it in the runloop. If the performselector is the same as the performselector: withdelay (when delay is 0), is the performselector asynchronous? Do you want to enter runloop?

A lot of cocoa users don't understand it at the beginning.

 

Tianya
A:
1. Yes. Do not upload mselector. Directly objc_msgsend. As a matter of fact, this is an improvement during OC compilation, that is, the call of the object is converted into the call of the function during OC compilation.
2. Yes. If delay is set to 0, it also enters the runloop. The advantage of this operation is that the execution of the method called receivmselector: withdelay: is not blocked.

How can I confirm that the answer above is correct?
It's easy to write a few tests.Code, And then interrupt the first run to view the call stack during running.
ObservationProgramThe best method to run is always the debugger ~

Respondstoselector

3. reasons why the delegate attribute uses assign.

Loop reference

All reference counting systems have the issue of circular application. For example, the following reference link:

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.

At this time, the reference count of B and C is 2 and 1 respectively. When a no longer uses B, call release to release the ownership of B. Because C also references B, the reference count of B is 1, and B is not released. B is not released. The reference count of C is 1, and C is not released. From then on, B and C are always in the memory.

In this case, you must interrupt the circular reference and maintain the reference relationship through other rules. For example, our common delegate is generally an assign attribute instead of a retain attribute. The value assignment does not increase the reference count. It is designed to prevent unnecessary cyclic references at the two ends of delegation. If a uitableviewcontroller object A obtains the ownership of uitableview object B through retain, the delegate of uitableview object B is a. If the delegate is retain, there is basically no chance to release these two objects. Pay attention to this when designing the delegate mode.

Memory leakage caused by cyclic references cannot be discovered by instrument, so be careful.

4. reasons why the delegate attribute uses assign.

There are also some usage that will give the system ownership of the object. For example, nsobject implements mselector: withobject: afterdelay. If necessary, call cancelpreviousperformrequestswithtarget: selector: object:. Otherwise, memory leakage may occur.

 

Dynamic call classes and methods during iPhone development:

Nsclassfromstring

Nsselectorfromstring

 

Normally,

Id myobj = [[nsclassfromstring (@ "myspecialclass") alloc] init];

And

Id myobj = [[myspecialclass alloc] init];

Is the same. However, if the class myspecialclass does not exist in your program, the following write method will fail, and the above method only returns an empty object.

Therefore, in some cases, you can use nsclassfromstring to initialize uncertain classes.

For example, nstask may occur on the iPhone, so it is best to use nstask:

[[Nsclassfromstring (@ "nstask") ......]

Instead of using [nstask.

The advantages of nsclassfromstring are:

1. weaken the connection,Therefore, no framework is linked to the program.

2. Import is not required,Because classes are dynamically loaded, they can be loaded as long as they exist.

 


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([NsstringStringwithformat: @ "Seta % I", I])];

}

}

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.