Declaration of member variables @interface and @property

Source: Internet
Author: User

just beginning to contact OC and then read the code written by others, often confused in the declaration of the attributes of the writing, summed up there are three ways, do not know which kind of comparative normalization, now I put three ways to paste out, and then one by one to discuss each way to declare the difference between attributes.

Method one: declared directly in the curly braces in @interface.

@interface mytest:nsobject{

NSString *mystr;

}


Way two: Declare in @interface, and then declare in @property.

@interface mytest:nsobject{

NSString *_mystr;

}


@property (Strong, nonatomic) NSString *mystr;

Then join in the. m file

@synthesize mystr = _mystr;


Way three: directly with @property declaration

@interface mytest:nsobject{

}


@property (Strong, nonatomic) NSString *mystr;

The @synthesize mystr = _mystr is then added to the. m file;


========================== I'm a split line ============================


First of all, the difference between the way a way three, the use of a declaration of the member variable is only within its own class use, and cannot be used outside the class,

(That is, by the class name.) Point of the way is not shown), the way three is the opposite, it can be accessed outside the class, within the class can be drawn by the line + variable name

or self. Variable name to access.


The way two is written is an obsolete way of declaring variables, and Xcode needs to be handwritten prior to the early @systhesize without automatic compositing of the properties.

Getter and setter method, underline style indicates that this is the internal variable of the class, if you need to use the variable directly, you need to use the way get or set.

After Xcode now has an auto-compositing property, the compiler will automatically help us generate an instance variable that starts with an underscore, so we don't have to declare the property at the same time.

with variables. We can declare a member property directly in the @property way, so it doesn't matter if you don't use @systhesize in. m files, and Xcode automatically generates getter and setter for you.

Individuals prefer to use mode three, which is recommended by the Apple Development template, and can also be seen in. m files without @systhesize.


Declaration of member variables @interface and @property

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.