@ Property, @ synthesize, and point syntax in objective-C

Source: Internet
Author: User

In objective-C, @ property, @ synthesize, and point syntax are related to two functions. One is the setter function, and the other is the getter function.

In the past, we used to define the setter and getter functions.

@interface Dog:NSObject{int age;}-(void)setAge:(int)newAge;-(void)age;@end@implementation Dog-(void)setAge:(int)newAge{age = newAge;}-(void)age{return age;}@end

Now we have a better method:
@ Property is a function declaration that enables the compiler to automatically generate setter and getter.

@ Sythesize: enables the compiler to automatically implement the setter and getter functions.

Now we can write it like this.

@interface Dog:NSObject{int age;}@property int age;@end@implementation Dog@synthesize age;}@end

When @ properpty is not a basic data type, you can add a parameter

Related point Syntax:

Dog. Age = 10;

Dogage = [DOG ahe];

The compiler expands dog. Age = 10 into [DOG setage: 10];

Expand dogage = dog. Age; To dogage = [DOG age];

The dot syntax is on the left of the equal sign. It is a setter function, and the dot syntax is a getter function on the right of the equal sign.

Refer to the previous generation's summary:

Syntax for declaring property: @ Property
(Parameter 1, parameter 2) type name;

@property(nonatomic,retain) UIWindow *window;  

There are three types of parameters:

Read/write attributes: (readwrite/readonly)

Setter semantics: (assign/retain/copy)

Atomicity: (Atomicity/nonatomic)

 

Each parameter has the following meanings:

 

Readwrite: generate the setter \ getter Method

Readonly: generate only simple getter without setter.

Assign: the default type. The setter method directly assigns values without retain operations.

Retain: the setter method performs the release old value on the parameter, and then retain the new value.

Copy: the setter Method for copy operations, the same as retain

Nonatomic: multi-thread prohibited, variable protection, and performance improvement

 

Parameter type

The retain and copy parameters are complex. The specific analysis is as follows:

 

Getter Analysis

1,

@property(nonatomic,retain)test* thetest;  @property(nonatomic ,copy)test* thetest;  

Equivalent code:

-(void)thetest  {    return thetest;  }  

2,

@property(retain)test* thetest;  @property(copy)test* thetest;

Equivalent code:

-(void)thetest  {      [thetest retain];      return [thetest autorelease];  }  

Setter Analysis

@property(nonatomic,retain)test* thetest;  @property(retain)test* thetest; 

It is equivalent:

-(void)setThetest:(test *)newThetest {      if (thetest!= newThetest) {          [thetestrelease];          thetest= [newThetest retain];      }  }  

2,

@property(nonatomic,copy)test* thetest;  @property(copy)test* thetest;  

It is equivalent:

-(void)setThetest:(test *)newThetest {      if (thetest!= newThetest) {          [thetest release];          thetest= [newThetest copy];  

Nonatomic

If multiple threads are used, two threads may wait for each other to lock up (you can find out the precautions for deprecation ). If there is no (nonatomic), that is, the default (atomic) will prevent such threads from being mutually exclusive, but will consume a certain amount of resources. Therefore, if it is not a multi-threaded program, simply add (nonatomic ).

 

Retain

Code Description

If it is only @ property nsstring * STR; then the setter code automatically generated by @ synthesize is:

-(void)setStr:(NSString*)value{      str=value;  }  

If it is @ property (retain) nsstring * STR; then the automatic setter content is:

-(void)setStr:(NSString*)v{      if(v!=str){          [str release];          str=[v retain];      }  }  

What I see here is a tangle

@ Synthesize
Window = _ window, which means that the window attribute is the _ window instance variable synthesis accessors method.
That is to say, the access method for generating the window property is setwindow, And the setwindow method is the access method for the _ window variable, which operates on the _ window variable.
The following is a common example:
@ Interface myclass: nsobject {
Myobjecct * _ myobject;
}
@ Property (nonamtic, retain) myobjecct * myobject;
@ End

@ Implementatin myclass
@ Synthesize myobject = _ myobject;
This class declares a Variable _ myobject and declares an attribute named myobject. Then, @ synthesize is used to generate an access method for the attribute myobject. The name of this access method should be: setmyobject and getmyobject. @ Synthesize
Myobject = _ myobject means that the access method of the property myobject is used for the variable _ myobject.
This method is common in Apple's sample code.

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.