OC Language Description Method and SEL

Source: Internet
Author: User

First, description method

The description method includes class methods and object methods. (Included in the NSObject Class)

(i) Basic knowledge

-Description (Object method)

When you use NSLog and%@ to output an object, the object's description method is called and the return value is returned for output.

+ Description (class method)

When you use NSLog and%@ to output a class, the description method of the class is called, the return value is returned for output, the entire object is printed once, and the print object uses%@.

Use%@ to print objects such as ("%@", p) The default printout is the < class name: memory address, although the string is also an object, but the string is special when printing with%@.

So how do you implement all the properties of the printed object? Override the description method in the implementation of the class.

(ii) Implement all properties of the Print object

(c) Difference

The + Description method determines the output of the class object, which is the class itself

-The Description method determines the output of the instance object, which is the object created by the person.

(iv) Printing related supplements

Second, SEL

SEL: Full name selector represents the location where the method is stored.

How is the method stored in memory?

Person *p = [[Person alloc] init];

[P test];

The process of finding a method:

(1) First, the test method name is packaged into SEL type data;

(2) Find the corresponding method address according to the SEL data;

(3) Call the corresponding method according to the method address.

(4) Note: During this operation, there is a cache, the first time to find a time to find, very consumption performance, and then use the time when it is used directly.

About _cmd: There is a _cmd inside each method that represents the current method.

Note: The SEL is actually a wrapper for the method, wrapping the method into an SEL type of data, finding the corresponding method address, and then calling the method when the method address is found. These are run-time features, and the message is to send the SEL, and then find the address based on the SEL and invoke the method.

   //  manually wrapping the Eat method into the SEL type  SEL S1 = @selector (eat);   [P2 perfomselector:s1] was called through S1 ;

OC Language Description Method and SEL

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.