IPhone development: Interpreting the parameter when @ property is declared in objective-C

Source: Internet
Author: User


The syntax for declaring property is:@ Property (attribute [, attribute 2,...])Type name;



There are three types of attributes:



1. read/write attributes (writability) include: readwrite/readonly



2. setter semantics (setter semantics) includes: Assign/retain/copy



3. atomicity (Atomicity) includes: nonatomic



The following describes the meaning of each attribute.



Readwrite/readonly:



Determines whether to generate the set accessors. readwrite is the default attribute and generates the getter and setter methods. readonly only generates the getter method and does not generate the setter method.
The readonly keyword indicates that the setter will not be generated, so it cannot be used in combination with copy/retain/assign.



Assign/retain/copy:



These attributes are used to specify the semantics of the Set accessors. That is, these attributes determine how to assign new values to data members.



Assign:



Direct value assignment, index count unchanged, applicable to simple data types, such as nsingeter, cgfloat, Int, Char, etc.



Retain:



The pointer copy uses the original memory space.
The index count of the object is increased by 1.
This attribute can only be used for the objective-C object type, but not for the core foundation object. (The reason is obvious: retain will increase the reference count of the object, and neither the basic data type nor the core foundation object has reference count ).



Copy:



Copy the object, apply for a new memory space, and copy the original content to that space.
The index count of the new object is 1.
This attribute is only valid for object types that implement the nscopying protocol.



In many objective-C objects, it is best to use retain. For some special objects (such as string), copy is used.




Nonatomic:



Non-atomic access without synchronization. multi-thread concurrent access improves performance. If this attribute is not added, both access methods are atomic transaction access by default. The default value is atomic, which is an atomic operation.
(Atomic is a thread protection technology used by objc. Basically, it is used to prevent reading by another thread when the write is not completed, resulting in data errors. This mechanism consumes system resources. Therefore, nonatomic is a good choice for small devices such as the iPhone, if multi-thread communication programming is not used .)



The syntax for declaring property is:@ Property (attribute [, attribute 2,...])Type name;



There are three types of attributes:



1. read/write attributes (writability) include: readwrite/readonly



2. setter semantics (setter semantics) includes: Assign/retain/copy



3. atomicity (Atomicity) includes: nonatomic



The following describes the meaning of each attribute.



Readwrite/readonly:



Determines whether to generate the set accessors. readwrite is the default attribute and generates the getter and setter methods. readonly only generates the getter method and does not generate the setter method.
The readonly keyword indicates that the setter will not be generated, so it cannot be used in combination with copy/retain/assign.



Assign/retain/copy:



These attributes are used to specify the semantics of the Set accessors. That is, these attributes determine how to assign new values to data members.



Assign:



Direct value assignment, index count unchanged, applicable to simple data types, such as nsingeter, cgfloat, Int, Char, etc.



Retain:



The pointer copy uses the original memory space.
The index count of the object is increased by 1.
This attribute can only be used for the objective-C object type, but not for the core foundation object. (The reason is obvious: retain will increase the reference count of the object, and neither the basic data type nor the core foundation object has reference count ).



Copy:



Copy the object, apply for a new memory space, and copy the original content to that space.
The index count of the new object is 1.
This attribute is only valid for object types that implement the nscopying protocol.



In many objective-C objects, it is best to use retain. For some special objects (such as string), copy is used.




Nonatomic:



non-atomic access without synchronization, multi-thread concurrent access improves performance. If this attribute is not added, both access methods are atomic transaction access by default. The default value is atomic, which is an atomic operation.
(atomic is a thread protection technology used by objc. Basically, it is used to prevent reading by another thread when the write is not completed, resulting in data errors. This mechanism consumes system resources. Therefore, nonatomic is a good choice for small devices such as the iPhone, if multi-thread communication programming is not used .)


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.