In objective-C, @ property and @ synthesize

Source: Internet
Author: User

Keyword in OC, @ property and @ synthesize pair.

Syntax:

@ Property (parameter 1, parameter 2) variable type variable name

@ Synthesize variable name

Function:

Let the compiler automatically compile a method declaration with the same name as the data member, which saves the need for declarative read/write methods.

For example:

// @ Property nsinteger number in the header file; // @ synthesize number in the implementation file;

Equivalent:

// In the header file-(void) setnumber :( nsinteger) newnumber;-(nsinteger) number; // In the implementation file-(void) setnumber :( nsinteger) newnumber {number = newnumber;}-(nsinteger) number {return number ;}

Obviously, using @ property and @ synthesize reduces the workload.

The following parameters are provided:

Nonatomic: atomicity, variable protection, multithreading prohibited, and performance improvement

Atomic: opposite to nonatomic

Retain: The old value of the release parameter in the setter method.

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

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

Readonly: only generate simple getter and do not claim the setter Method

Readwrite: generate the setter and getter methods.

Atomic means that the setter/getter function is a primitive operation. If multiple threads call setter at the same time, there will be no execution of setter by another thread before executing all the setter statements, which is equivalent to adding a lock at the beginning and end of the function.
Nonatomic does not guarantee primitive operations of setter/getter, so you may get incomplete things. For example, if you use nonatomic to change two member variables in the setter function, Getter may obtain the state when only one of the variables is changed, which may cause problems. If multithreading is not required, nonatomic is enough. In addition, since the lock operation is not involved, it is faster to execute.

Example:

1. Retain

@ Property (nonatomic, retain) class * Aclass; @ synthesize Aclass; // equivalent to:-(void) setaclass :( class *) _ Aclass {If (Aclass! = _ Aclass) {[Aclass release]; Aclass = [_ Aclass retain] ;}}-(class *) Aclass {return Aclass ;}

2. Retain

@ Property (retain) class * Aclass; @ synthesize Aclass; // equivalent to:-(void) setaclass :( class *) _ Aclass {If (Aclass! = _ Aclass) {[Aclass release]; Aclass = [_ Aclass retain] ;}}-(class *) Aclass {[Aclass retain]; return [Aclass autorelock];}

3. Copy

@ Property (nonatomic, copy) class * Aclass; @ synthesize Aclass; // equivalent to:-(void) setaclass :( class *) _ Aclass {If (Aclass! = _ Aclass) {[Aclass release]; Aclass = [_ Aclass copy] ;}}-(class *) Aclass {return Aclass ;}

4. Copy

@ Property (copy) class * Aclass; @ synthesize Aclass; // equivalent to:-(void) setaclass :( class *) _ Aclass {If (Aclass! = _ Aclass) {[Aclass release]; Aclass = [_ Aclass copy] ;}}-(class *) Aclass {[Aclass retain]; return [Aclass autorelease];}
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.