The @property in Objective-c

Source: Internet
Author: User

1: What is @property?

In Objective-c, @property is the syntax for declaring attributes, which makes it quick and easy to create getter/setter methods for instance variables.

2: The nature of @property?

@property = Ivar + setter + getter, is actually the instance variable + getter Method + Setter method.

3: The role of @property?

@property (attributes) as an attribute of objective-c, the main function is to encapsulate the data in the object. The instance variables in Objective-c are usually accessed through the Getter/setter method. For an instance variable of a given name in an object, the compiler automatically generates the Getter/setter method.

@property is usually used in the work as follows:

@interface**cartype; @end

The above code is equivalent to the following code:

@interface car:nsobject{    //  Declaration of two instance variables    nsstring *carname;     *Cartype;} -(void) Setcarname: (NSString *) carname; -(NSString *) carname; -(void) Setcartype: (NSString *) Cartype; -(NSString *) Cartype; @end

Visible: Declares an instance variable and declares the access method that implements the variable, and the effect is the same as @property.

4: How is the @property,ivar, getter, setter generated and added to the class?

After you declare a property with @property, the compiler automatically generates an instance variable, and an access method for that instance variable. This process is called automatic synthesis (autosynthesis). The process is completed at compile time, so the source code of the access method is not visible in the compiler.

5: What is the name of the @property generated instance variable?

If you do not use @synthesize to specify the name of the instance variable, the name of the instance variable that is generated by default using @property is the underscore + property name. For example, in the code above:

@property (nonatomic, copy) NSString **cartype;

The instance variable names generated during compilation are _carname, _cartype, and _carname, _cartype can be used directly in the program.

You can use the @synthesize to specify the name of the instance variable, as follows:

@synthesize carname = mycarname; @synthesize cartype = Mycartype;

In this way, the two instance variable names generated in the program are Mycarname, Mycartype, and can be used directly.

If @synthesize is used, but the instance variable name is not specified, as follows:

@synthesize Carname,cartype;

In this case, the two instance variable names generated in the program are Carname, Cartype, and can be used directly.

The @property in Objective-c

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.