The declaration list in property has been classified into the following categories:
1. Declare the access method of the attribute:
Getter = getterName
Setter = setterName
Declares the access attribute settings and method names.
2. Declare the permission for 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. Declare the implementation of the write method:
Assign
The setter method uses direct value assignment to set values. For example:
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:
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:
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.
Summary: variables requiring global access should be declared as attributes for assignment as much as possible, and member variables cannot maintain consistency of reference count, resulting in crash, it is acceptable to use member variables or attributes when accessing data internally. When assigning values to attributes, the set Method of the attribute is called to add one reference count to ensure correct memory reference.