iOS basic knowledge attributes and attribute keywords

Source: Internet
Author: User

iOS Attributes and attribute keywords


First, the property function:
1. Generate a pair of setter/getter methods for existing member variables.
2. If no member variable is declared, a private variable of the property name is automatically declared (the default member variable is protected).


Second, the attribute keyword:
Assign the default (default) keyword, the assignment of the base data type.
MRC: Manage Memory manually
Retain the property declaration of the object, saves the reference count, and if other objects use the current object, the object's counter is incremented by 1, which means that two objects point to the same piece of memory at the same time.
The property declaration of the Copy object, which copies the object directly to a new copy, and the counter of the copied object does not add 1, that is, two objects point to different memory, except that the data in memory is the same.

ARC: Automatically manage memory
Strong strong pointer, increase the reference permission for an object (reference count plus 1)
Weak a weak pointer does not increase the object's referential permissions, and does not affect whether the object is disposed.
Strong and weak are applied to the properties of the object type
Retain,copy can be used under arc.

Thread-related
Nonatomic a single-threaded program's property declaration, the default keyword, single-threaded use, and high execution efficiency.
Atomic a multi-threaded program's attribute declaration, it is not recommended to use atomic to handle thread safety and perform inefficiently.

Keywords for property Read permissions
ReadWrite generate getter and setter methods, default permissions
ReadOnly only generates getter methods, which are accessible only and cannot be assigned values.


Third, the use of the attribute keyword:

Non-object types use assign to omit
Object type Use Retain/strong
NSString using Copy/strong

For example:

1 @property (nonatomic, assign) cgfloat duration; 2 @property (nonatomic, Strong) Glprogram * program; 3 @property (nonatomic, copy) NSString *dirpath;

iOS basic knowledge attributes and attribute keywords

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.