2. Property details (@property/@dynamic/@synthesize)

Source: Internet
Author: User

As mentioned in the previous article, after OC 2.0 attributes are declared, if there is no readonly modifier, the current class automatically generates a declaration of the setter and getter methods, and the corresponding instance variable (underscore + attribute name) is automatically generated. Setter and Getter are the methods to access this instance variable.

@property are divided into @property and @dynamic or @synthesize: @property, written in the @interface of the. h file, the declaration of the completion property @dynamic or @synthesize, written in the. m file @ In implementation, the completion property is implemented @dynamic meaning to tell the compiler that instance variables and access methods for this property are implemented by the user themselves and are not generated automatically. @synthesize means that the default instance variable is automatically added by the compiler and the corresponding access code is generated to satisfy the property declaration.

. h file
@property (nonatomic) Nsinteger age;
@property (nonatomic,copy) nsstring *name;
@property (Nonatomic,retain) nsnumber *number;

In the. m file
@synthesize name = _name, age = _age, number = _number;
The statement is not written and is automatically generated by default, representing the implementation of all setter and getter methods

In normal use, the corresponding @synthesize are automatically generated in. m after @property in. h.

The role of @synthesize:
To add a member variable corresponding to an attribute
2. Member variables that are manipulated by the setter getter method that specifies the attribute declaration
3. If @ synthesize is omitted, the setter and getter method of the corresponding property is automatically generated, and the member variable of the default action is ' _ ' + property name
4. Detect manual implementation of the @synthesize, you will generate the member variable name according to your requirements and generate corresponding setter getter method, such as @synthesize name = _myname; So the member variable is _myname.
5. The setter and getter methods are not automatically synthesized in the following situations and do not detect the existence of member variables and do not help us generate corresponding member variables, we need to add member variables ourselves: 1> when the setter and getter are rewritten; 2> when a getter with a read-only property is overridden; 3> when the @dynamic is used; 4> all attributes defined in the @protocol; 5> all attributes defined in the category; 6> overrides the property, and when you override the attribute in the parent class in the subclass, you must use the @synthesize to manually synthesize the Ivar.

Notes on @property:
1. Read and write features
(1) ReadWrite (readable writable) default properties, generating setter methods and generating Gette methods
(2) readonly (read only), generates getter methods only, does not generate setter methods
(3) Setter = method Name, a different method name for the generated setter method
(4) Getter = method Name, a different method name for the generated getter method

2. Atomic characteristics
(1) Atomic (atomic attribute) default attribute, which guarantees thread safety, that is, the current instance variable can only be accessed by one thread, in which there is a lock-unlock process

3. Semantic features
(1) Assign default properties, typically used to decorate a property of a non-object type
(2) Retain the object's reference count of +1
(3) Copy a copy of the object for the object, creating a new object
// Note: General Nsstring/block needs to add a copy property for its properties, and other objects to add the Retain property

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.