Attributes in objective-c

Source: Internet
Author: User

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.

 


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.