19-oc@property and @synthesize

Source: Internet
Author: User

@porperty

@porperty is a compiler directive

Before Xocde4.4, you can use @porperty to replace the declaration of the Getter/setter method, that is, we only need to write @porperty to write the declaration of the Getter/setter method

2. Compiler as long as you see @property, you know we're going to generate a declaration of a property's Getter/setter method

@propertyde format

@property data type variable name

Property enhancement

Since the Xcode4.4, the @property has been enhanced to generate the declaration and implementation of Setter/getter methods at the same time as long as a @property can be exploited.

If you do not tell @property to assign the passed-in parameter to WHO, the default @property assigns the passed-in property to the member variable that begins with _

@property Usage Scenarios

If you do not want to filter the incoming data, just provide a method to the outside operating member variables, then you can use @property, and the system will automatically give us a _ start of the member variable

When to override the Getter/setter method when using property enhancement

With property enhancement, only the declaration and implementation of the simplest Getter/setter method is generated, and the incoming data is not filtered

If you want to filter incoming data, then we have to rewrite the Getter/setter method

What are the points of note for overriding the Getter/setter method

If the setter method is overridden, the property will only generate getter methods

If the getter method is overridden, the property will only generate setter methods

If the Getter/setter method is overridden at the same time, the property does not automatically generate a private member variable for us

@property modifiers

Enhanced @property format after use of modifiers

Format:

@property (property modifier) data type variable name;

What modifiers do @property have?

1.readwrite: Represents both Getter method generation and setter method generation

By default, @property is ReadWrite.

@property (readwrite) int age;

2.readonly: Represents only generation getter method does not generate setter method

@property (readonly) NSString * name;

3. An alias for the Getter method is ABC

-(void) SetHeight: (double) height;

-(double) height;

An alias for the Getter method is ABC

-(void) SetHeight: (double) height;

-(double) ABC;

@property (GETTER=ABC) double height;

4. An alias for the setter method is Tizhong

-(void) Setweight: (double) weight;

-(void) Tizhong: (double) weight;

@property (Setter=tizhong:) double weight;

There is a convention between programmers, generally get the value of the attribute of type bool, we will change the name of the obtained method to Isxxx

@property (getter=ismarried) BOOL married;

@synthesize

Synthesize is a compiler directive that simplifies the implementation of our getter/setter approach

@synthesize age = _age; What does the compiler do when assigning an age value?

@synthesize age = _age;

1. In age after @synthesize, tell the compiler which @property generated declaration needs to be implemented

2. Tell @synthesize that you need to assign the value passed in to whom and return the value to the caller

If you do not tell the system after the @synthesize to assign the value to whom, the system defaults to the @synthesize with the same name as the member variable that is written after @synthesize age;

[email protected] and @synthesize

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.