IOS OC Declares the difference between @interface brackets and the use of @property

Source: Internet
Author: User

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;

First of all, the difference between the way a way three, the use of a declared member variable is only within its own class use, and cannot be used outside the class, (that is, through the class name.) Point is not shown), the way three is the opposite, it can be accessed outside the class, inside the class can be accessed by the underscore + variable name or self. Variable name.

The way two is written is an obsolete way of declaring variables, and Xcode requires a handwritten getter and setter method in the early @systhesize without the automatic compositing of the property, underlining the style that this is the internal variable of the class. If you need to use variables directly, you need to use the Get or set method.

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 and the variable at the same time. 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.

IOS OC Declares the difference between @interface brackets and the use of @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.