Objective-C learning notes-Part 10-Selector

Source: Internet
Author: User

In object-C, selector has two meanings: In the source file, it points to a method call, and after compilation it points to an unque indentifier.

The type of the compiled selector is Sel, And the selector of the same name is the same.

Using selector to call the object method is the basis of the "target-action" programming model of the cocoa framework.

You can use @ selector to create an alias for selector:

Sel setwidthheight;
Setwidthheight = @ selector (setwidth: height :);

Using the alias method to reference seletor during compilation will not significantly affect the performance. Which of the following methods can be called dynamically based on a word string during running is similar to reflection:
Nsstring * method;
Method = nsstringfromselector (setwidthheight );

Methods and selectors

The framework only assigns identifier handler to the selector, which is not the implementation of the method. Therefore, although the identifier of the selector is the same for methods of different classes, the execution is similar to the method call. Static methods can also be renamed with dynamic methods because they have different owner objects.

For dynamic type annotation, the method with the same name must be of the same parameter type and return value, but not for static type annotation. This is because the implementation of the method can be known through the metadata of the class during static type annotation.

In addition, static methods and instance methods are not restricted. They exist in different spaces.

Id helper = getthereceiver ();
Sel request = gettheselector ();
[Helper listener mselector: request];

For example, some dynamic calls seem very powerful.

[Mybuttoncell setaction: @ selector (reapthewind :)];
[Mybuttoncell settarget: anobject];

The aboveCodeDemonstrate how to associate buttons with event processing objects

Although there are some ways to use dynamic message to maintain the relationship between buttons and event processing objects, Apple believes that it will bring unnecessary complexity, so the current method is used.

To prevent an object from receiving messages that it cannot process, it is always good to use the static annotation type because it can be checked during compilation.

If you must call an uncertain method at runtime, you can perform the test in this way to make a safe call instead of reporting an error:

If ([anobject respondstoselector: @ selector (setorigin: :)])
[Anobject setorigin: 0.0: 0.0];
Else
Fprintf (stderr, "% s can't be placed \ n ",
[Nsstringfromclass ([anobject class]) utf8string]);

In addition, when an object cannot process a message, it can forward it. For more information, see message forwarding: http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html#//apple_ref/doc/uid/TP40008048-CH105

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.