Objective-C attribute keywords (assign, retain, copy, readonly, readwrite, atomic, nonatomic)

Source: Internet
Author: User

In objective-C, attributes are usually set with the following keywords:

-- Assign: Specifies the setter method to use a simple value assignment, which is the default operation. You can use this attribute for scalar type (such as INT. You can imagine a float. It is not an object, so it cannot be retain or copy.

-- Retain: specifies that the retain should be called on a later object. The previous value sends a release message. You can imagine an nsstring instance, which is an object and you may want to retain it.

-- Copy: specifies that the object copy (deep copy) should be used. The previous value sends a release message. Basically like retain, but does not increase the reference count, it is to allocate a new memory to place it.

-- Readonly: only the getter method is generated instead of the setter method (the getter method does not have the get prefix ).

-- Readwrite: the default attribute. The getter and setter methods without additional parameters are generated (the setter method has only one parameter ).

-- Atomic: the default attribute of the object, that is, the method generated by setter/getter is an atomic operation. If multiple threads call setter at the same time, there will be no execution of Setter in another thread before executing all the setter statements. It is related to the locking at the beginning and end of the method.

-- Nonatomic: the atomicity of setter/getter is not guaranteed. data may be faulty in the case of multiple threads.

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.