@ Property and @ synthesize usage 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
  1. @ Property int count;

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

C code
  1. -(INT) count;
  2. -(Void) setcount :( INT) newcount;

 

2. Implementation file (. m)

C code
  1. @ Synthesize count;

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

C code
  1. -(INT) count
  2. {
  3. Return count;
  4. }
  5. -(Void) setcount :( INT) newcount
  6. {
  7. Count = newcount;
  8. }

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
  1. @ 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
  1. @ Property (nonatomic, retain) test * thetest;
  2. @ Property (nonatomic, copy) test * thetest;

Equivalent code:

C code
  1. -(Void) thetest
  2. {
  3. Return thetest;
  4. }

 

2,

C code
  1. @ Property (retain) test * thetest;
  2. @ Property (copy) test * thetest;

Equivalent code:

C code
  1. -(Void) thetest
  2. {
  3. [Thetest retain];
  4. Return [thetest autorelease];
  5. }

 

Setter Analysis

 

1,

C code
  1. @ Property (nonatomic, retain) test * thetest;
  2. @ Property (retain) test * thetest;

It is equivalent:

C code
  1. -(Void) setthetest :( test *) newthetest {
  2. If (thetest! = Newthetest ){
  3. [Thetestrelease];
  4. Thetest = [newthetest retain];
  5. }
  6. }

2,

C code
  1. @ Property (nonatomic, copy) test * thetest;
  2. @ Property (copy) test * thetest;

It is equivalent:

C code
  1. -(Void) setthetest :( test *) newthetest {
  2. If (thetest! = Newthetest ){
  3. [Thetest release];
  4. Thetest = [newthetest copy];
  5. }
  6. }

 

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
  1. -(Void) setstr :( nsstring *) value {
  2. STR = value;
  3. }

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

C code
  1. -(Void) setstr :( nsstring *) V {
  2. If (V! = Str ){
  3. [STR release];
  4. STR = [V retain];
  5. }
  6. }

 

 

Owner attributes

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

Attribute Value keyword ownership

Strong _ Strong Yes
Weak _ Weak None
Unsafe_unretained _ Unsafe_unretained None
Copy _ Strong Yes
Assign _ Unsafe_unretained None
Retain _ Strong Yes

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. For example:
C code
  1. -(Void) setname :( nsstring *) _ name {
  2. Name = _ name;
  3. }
 
  • Retain
    Declaration in the setter method, you need to perform the retain plus 1 operation on the set value. For example:
C code
  1. -(Void) setname :( nsstring *) _ name {
  2. // First, determine whether it is consistent with the old object. If it is inconsistent, assign a value.
  3. // 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.
  4. If (name! = _ Name ){
  5. [Name Release];
  6. Name = [_ name retain];
  7. }
  8. }
 
  • 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, for example:
  • C code
    1. [_ Internal lock]; // lock using an object-Level Lock
    2. Id result = [[value retain] autorelease];
    3. [_ Internal unlock];
    4. 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.