Dark Horse Programmer--dot syntax, description, SEL, ID use

Source: Internet
Author: User

Dark Horse Programmer--dot syntax, description, SEL, ID use

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Introduction and use of point syntax

**p.age;**

Object. Property name

Note: At this point, the (object. property name) does not directly access the instance variable. Instead of seeing the dot syntax, we automatically replace the p.age=18-replacement with [P setage:18];

*************************

int a=p.age;//Call Get method

Note: When you see P.age appearing on the right side of the equal sign, it is automatically replaced with p.age--replacement-->[p age];

Emphasis: Whether P.age is replaced with a get or set method, usually depends on whether P.age appears on the left or right side of the equals sign (whether it is a set value or a value assignment)

Dead Loop:

Set Method:

-(void) Setage: (int) NewAge {

The following code will cause a dead loop

Self.age = NewAge;

}

Equivalent

-(void) Setage: (int) NewAge {

[Self setage:newage];

}

Dead Loop:

In the Get method, return self.age is equivalent to [self-age];

********************************

Description Method Introduction and rewriting

When the object or class is printed in%@ format, the object's description method is called. Object, if the parent class's description method is not overridden, the parent class is called

********************************

Where the SEL method is stored

Is the packaging of the method, packaged into SEL type data

Find the corresponding method address based on the SEL data

Call the appropriate method based on the method address

Example: Wrapping the test method into an SEL type

SEL S1 = @selector (test);

Use:

[P performselector:test];

*********************************

Coercion Type Conversion Example:

NSObject *obj=[animal New];

[(animal*) obj run];

[(Subclass name *) pointer name behavior]

*********************************

ID using:

There is no need to enforce type conversions, which can be used directly, as is the dynamic type that the compiler checks for types.

ID obj;

Obj=dog;

[obj Run]

Equivalent to NSObject *

Instancetype and ID

Same: Can be used as the return type of a method

Different:

Instancetype can return objects of the same type as the method. ID can only return objects of unknown type

Instancetype can be used only as a return value, and an ID is also available as a parameter

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Dark Horse Programmer--dot syntax, description, SEL, ID use

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.