OC for iOS Development (10)-nature of the class, description, SEL

Source: Internet
Author: User

(a) The nature of the class

1. Class is also an object

In fact, the class is also an object, is the class type, referred to as " class object ".

// definition of class type struct objc_class *class; // The class name represents the class object, and each class has only one class object

2.+load and +initialize

    • +load

> loads all classes and classifications when the program starts, and invokes the +load method for all classes and classifications

> First loads the parent class and then loads the subclasses, that is, the +load of the parent class is called first, and then the +load of the subclass is called.

> Load Primitive Classes First, then load categories

> Whether or not this class is used by the program's running process, it calls +load to load

    • +initialize

> When a class is first used (such as creating objects, etc.), the +initialize method is called once

> A class will call the +initialize method only once, call the parent class first, and then call the subclass's

code Example // when the program starts, it loads all the classes in the project at once. After the class is loaded, the +load method + (void) load{    NSLog (@ "person---load" is called) );} // when this class is first used, it is called once +initialize method + (void) initialize{    NSLog (@ " person-initialize " );} @end

3. Two ways to get class objects

class];    //   class method //  or newclass];     //   Object Methods

4. Class object Invocation class method

Classnew];

(ii) Description method

( determines the output of an instance object/class object )

1.-description method

When an object is output using NSLog and%@, the object's-description method is called and the return value is returned for output

1. + Description Method

When you use NSLog and%@ to output a class object, the Class object +description method is called, and the return value is obtained for output

2. Modify the default output of the NSLog

Problem arises

int Main () {    *p = [[Person alloc] init];    NSLog (@ "%@", p);         return 0 ;}    The output is:0x1001033b0>   is an address, want to output the specific content

rewrite The-description or +description method to

3. Dead Loop Traps

If you use NSLog to print self in the-description method

(iii) SEL

/* The sel is actually a wrapper around the method, wrapping the method into an SEL type of data to find the corresponding method address.   

Principle//1. to wrap a method such as Test into a sel type of data

//2. find the corresponding method address according to the SEL data

//3. Call the corresponding method according to the method address

*/

1. Location of method Storage

> List of methods for each class is stored in the class object

> Each method has an object of the corresponding SEL type

> based on a Sel object, you can find the address of the method, and then call the method

Definition of >sel type

typedef struct OBJC_SELECTOR *sel;

2.creation of SEL objects

SEL s = @selector (test);

SEL s2 = nsselectorfromstring (@ "Test");

Other uses of 3.SEL objects

code example

There are two ways to invoke a method:1) directly through the method name [P test2]2 The SEL that the incoming method corresponds to to find the method address //To call the test method indirectly[P performselector: @selector (Test)];3) object method to pass (with) the parameter[P test:@ "123"]; Equal to the following (SEL's creation sel s =@selector (test:); )[P performselector: @selector (test:) withobject:@ "456"];1>Turn the string into Selsel s=nsselectorfromstring (@ "name");2>Change the Sel object to a string NSString object_cmd represents the current method ([email protected] (test); )nsstring *str = Nsstringfromselector (@selector (test)); NSString*str =Nsstringfromselector (_cmd); add: There is a hidden SEL data inside each method: _cmd (SEL) represents the current method dead loop- (void) test{//_cmd represents the current approach//will trigger a dead loop .[self performselector:_cmd]; }

OC for iOS Development (10)-nature of the class, description, SEL

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.