iOS Programming Learning (2): copy,nonatomic, Retain,weak,strong usage

Source: Internet
Author: User
Tags list of attributes

Property properties?
Property: Provides a declaration of access methods for member variables, access to control member variables, and access to member variables that control multi-threading. The property can be used not only in interface, but also in the Protocol protocol. and categories category.


Synthesize synthetic accessor method?
Implements the definition of the method declared by the property. In fact, straightforward is like this: property declares the access method for some member variables, and synthesize defines the method declared by the property. Their previous correspondence is the method defined in the property declaration method----------"header file synthesize definition method---------" CPP file.


@property (attribute1, Attribute2, ...]) Is the official expression of the property, so see Attribute1, Attribute2, you should understand, his usage is not very simple. The following is a breakdown of his list of attributes:
The following is a brief introduction to the list of attributes, followed by code.
1. Readability: readonly, ReadWrite
@property (ReadWrite,....) ValueType value, which is the default property of a variable, that is, if you (ReadWrite and readonly are not used, then your variable is the ReadWrite property), By adding the ReadWrite attribute to your variable there will be a get method, and a set method.
@property (readonly,...) ValueType value; This property variable means that the variable is only a readable method, that is, you can only use its Get method.
The 2,assign,setter method is assigned directly, without any retain operation, in order to solve the problem of the original type and the circular reference
3,retain,setter method to release the old value of the parameter and then retain the new value, all implementations are in this order
4,copy,setter method copy operation, as with the retain process, the old value of release, and then copy the new object, Retaincount is 1. This is the mechanism introduced to reduce dependency on the context.
5,nonatomic, non-atomic access, no synchronization, multi-threaded concurrent access improves performance.
Note that if you do not add this property, the default is two access methods are atomic transaction access. Locks are added to the owning object instance level. So no nonatomic is safe with multithreading.
6, strong weak
Strong and weak are the new object variable properties introduced by arc
ARC introduces a new life-cycle qualification for new objects, which is 0 weak references. If the object that the 0 weak reference points to is deallocated, the 0 weakly referenced object is automatically set to nil.


@property (strong) MyClass *myobject;
Equivalent to @property (retain) MyClass *myobject;


@property (weak) Myotherclass *delegate;
Equivalent to @property (assign) Myotherclass *delegate;


The generalized difference between a strong reference and a weak reference:
The survival of the object is directly determined by the existence of strong references, which are often referred to as references. If a reference to an object does not exist, and the object no longer appears in the list, the object is freed from memory.
Weak references are the same as strong references, except that they do not determine the survival of the object. Even if an object is held by an infinite number of references, it will be cleared if no strong reference is directed to him. No way, or "strong brother" have face.


Simply speaking strong equals retain
Weak more than assign a function, when the object disappears automatically turn the pointer to nil, the benefits are self-evident.

iOS Programming Learning (2): copy,nonatomic, Retain,weak,strong usage

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.