@ Property and @ synthesize in objective-C

Source: Internet
Author: User

@ Indicates the "objective-c" flag, proving that you are using the objective-C Language

Objective-C language keyword, @ property and @ synthesize pair.

Function:Let the compiler automatically write a method declaration with the same name as the data member to save the declaration of the read/write method.

For example:

1. In the header file:

C code

@property int count;  

It is equivalent to declaring two methods in the header file:

C code
- (int)count;  -(void)setCount:(int)newCount; 

2. Implementation file (. m)

C code
@synthesize count;

It is equivalent to implementing two methods in the implementation file (. m.

C code
- (int)count  {      return count;  }  -(void)setCount:(int)newCount  {      count = newCount;  }  

The above equivalent functions are automatically filled by the compiler to help developers, simplifying the coding input workload.

Format:

The syntax for declaring property is: @ property (parameter 1, parameter 2) type name;

For example:

C code
@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,

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

Equivalent code:

C code
-(void)thetest  {    return thetest;  }  

2,

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

Equivalent code:

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

Setter Analysis

1,

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

It is equivalent:

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

2. C code

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

Equivalent to: C code

-(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: C code

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

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

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

Owner attributes

Let's take a look at the mappings between attributes and keywords related to ownership.

Attribute Value keyword ownership

Strong

The _ strong keyword corresponding to the attribute, that is, the variable declared by this attribute will become the owner of the object.

Weak

This attribute corresponds to the _ weak keyword, which is consistent with the variable defined by _ weak. The variables declared by this attribute do not have the ownership of the object, and when the object is discarded, the object is automatically assigned a value of nil.

In addition, delegate and outlet should be declared using the weak attribute. At the same time, for example, the previous version of IOS 5 does not have the _ weak keyword, so the weak attribute cannot be used. In this case, we use unsafe_unretained.

Unsafe_unretained

It is equivalent to the variable declared by the _ unsafe_unretaind keyword. As described above, the system before iOS 5 uses this attribute to replace weak.

Copy

The difference with strong is that the declaration variable is the holder of the copy object.

Assign

Scalar varible is declared with this attribute, such as int and bool.

Retain

This attribute is consistent with strong, but it is more readable.

Refer:

Http://blog.eddie.com.tw/2010/12/08/property-and-synthesize/

Http://www.cocoachina.com/bbs/read.php? Tid = 7322

Http://www.cnblogs.com/pinping/archive/2011/08/03/2126150.html

Declared category

In the official objective-C document, the property section describes the class property in detail.
@ Property the declaration list has been classified into the following categories:

1,Access Method for declaring attributes:

  • Getter = gettername
  • Setter = settername
    Declares the access attribute settings and method names.

2,Permission for declaring attribute write operations:

  • Readwrite
    Declare this attribute as a read/write attribute, that is, you can access the setting method (setter), or you can access the getter method (getter), which is mutually exclusive with readonly.
  • Readonly
    Declare this attribute as a read-only attribute and only access the getter corresponding to this attribute. It is mutually exclusive with readwrite.

3,Implementation of declarative write methods:

  • Assign
    The setter method uses direct value assignment to set values. Example: C code
-(void)setName:(NSString*)_name{       name = _name;  }  

  • Retain
    Declaration in the setter method, you need to perform the retain plus 1 operation on the set value. Example: C code
-(Void) setname :( nsstring *) _ name {// first, determine whether it is consistent with the old object. If it is inconsistent, assign a value. // If it is an object, executing the code in if will cause an extreme situation: When the retain of this name is 1, this set operation allows the Instance name to be released in advance without assigning values. If (name! = _ Name) {[Name Release]; name = [_ name retain] ;}}

  • Copy
    Call the copy method of this instance to set the cloned object. Refer to retain for implementation.

4,Atomicity of access methods:

    • Nonatomic
      By default, the setter and getter implemented through synthesized are atomic access. Multi-threaded concurrent access ensures that the access method is only accessed by one thread at the same time, such as: C code
[ _internal lock ]; // lock using an object-level lock  id result = [ [ value retain ] autorelease ];  [ _internal unlock ];  return result;

  • However, if nonatomic is set, attribute access is non-atomic.

Source: http://wiki.magiche.net/pages/viewpage.action? Pageid = 1540101

@ Synthesize tabbarcontroller = _ tabbarcontroller;

@ Synthesize defines the names of getter and setter, which are different from the variable names, so as to protect the variables from improper access.

@ Property and @ synthesize usage 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.