OC midpoint syntax, property, and synthesize usage

Source: Internet
Author: User

I. verbose in oC

1> basic usage of point Syntax: · use objects. member variables can be used to set the value of member variables and obtain the value of member variables. 2> the essence of the dot syntax (the dot syntax is a function that the xcode compiler can help us) in fact, the dot syntax is the set and get methods. When the dot syntax is compiled, the compiler converts the dot syntax into the set and get methods. Note: If the set and get methods are not implemented in the class, there is no syntax p. name = @ ""; actually, it is [p setname: @ ""]; there is only one way to access member variables in oc-> such as Stu-> age, in this case, @ public is required. 3> Note: The following is an endless loop:

(1) In the set method, self. Age = age; equivalent to [self setage: Age];

(2) In the get method, return self. age; equivalent to [self age]; that is to say, in the set and get methods, the (self point member variable) cannot be used to create an endless loop. Of course, if you access the member variable, you can use the _ member variable. Example:-(void) setage :( INT) Age {_ age = age; // cannot be written as self. age = newage, equivalent to [self setage: newage];} self. age = age (error, endless loop)} 2: Use of the property and synthesize keywords Note: These two keywords are compiler features, so that xcode can automatically generate the getter and setter declarations and implementations. 1) @ propery int age. Before 4.4, we helped declare the get and set methods of age.

This line is automatically extended into the following two sentences during compilation:

-(Void) setage :( INT) age;

-(INT) age;

 

2) @ synthesize age. Before 4.4, we can implement the get and set methods.

The. M file helps us generate a private variable Age.

Variables in. m cannot be inherited.

@ Synthesize age = _ age; indicates that the get and Set Methods specify the value of _ age.

Of course, before 4.4, We need to declare (_ member variable)

3) @ property int age;

After xcode4.4

1) Declare the get and set methods of age

2) implement age get and set methods

 

3) generate a _ age (. h Declaration) (we do not need to declare it separately (_ member variable)

 

Note:

1. The member variables generated in this way are private. (So we can access the member variables modified by @ property through the dot syntax (get and set) in the subclass or outside)

2. You can add int _ age in {}. The declared _ age displayed is protected.

3. Principle: The get and set methods are the same as the variables. If you define them yourself, use the ones you have defined. If they are not defined, generate one automatically.

4. Manual implementation:

1) if the set method is manually implemented, the compiler will only generate the get method and member variables;

2) If you manually implement the get method, the compiler will only generate the Set Method and member variables;

3) if the set and get methods are both manually implemented, the compiler will not generate member variables.

5. In @ property, only the get method is used for the member variables modified by readonly, that is, only the value cannot be changed,

Currently, a problem occurs. The nsstring member Attribute Modified by readonly can be passed through (_ member variable) in. M, but cannot be accessed in. m (_ member variable) in the UI control)

 

 

 

 

 

OC midpoint syntax, property, and synthesize usage

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.