OC Language Description Method and SEL
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 an object, the description method of the class object 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 around the method, wrapping the method into an SEL type of data, finding the corresponding method address, and then calling the method after locating the method address. 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.
Li Hongqiang iOS Development OC Language description method and SEL