Nonatomic, Retain,weak,strong usage explanation

Source: Internet
Author: User
Tags list of attributes

Strong weak

Strong and weak are the new object variable properties introduced by Arc, which introduces new life-cycle qualifiers for new objects, or 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.

__weak, __strong is used to modify variables, in addition to __unsafe_unretained, __autoreleasing are used to modify variables.
__strong is the default keyword.
__weak declares a weak reference that can be automatically nil.
__unsafe_unretained declares a weak application, but does not automatically nil, that is, if the area of memory pointed to is released, the pointer is a wild pointer.
__autoreleasing is used to modify the parameters of a function, which is automatically released when the function returns.

Before writing a geo-location-based application, the application cannot be opened for positioning, and in the. h file, the attributes I define are:

@property (Weak, nonatomic) Cllocationmanager *locationmanager;

Later, I changed the above sentence to read:

@property (nonatomic, retain) Cllocationmanager *locationmanager;

You can do it.

Next, let's introduce nonatomic in detail, Retain,weak,strong
When we develop iOS programs, we often encounter:
property and synthesize, before very lazy did not carefully understand, just read someone else wrote the book, feel very easy (here I have to say, now a lot of native out of the Earth book, especially in the early 2009, write is really messy, fraught), so today there is time, I tried some, I hope to discuss it with you. property, he can provide the ability to provide a declaration of access methods for member variables, to control access to member variables, and to control the access environment for member variables when multi-threading occurs. Property can be not only in interface, in agreement [Url=]protocol[/url]. and category [Url=]category[/url] You can also use the. Synthesize in the understanding that implementing 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 was the method declared in the property declaration method----------"header file synthesize definition method---------" CPP file
But there's a slight difference here, and I'll talk about it later.
First, we all know: @property (attribute1, Attribute2, ...]) is @property his official expression way, so see attribute1, Attribute2, you should understand, his usage is not very simple. The following is a list of his attributes: The following is a brief introduction to the list of attributes, followed by code to explain. 1. Readability: readonly, [email protected] (ReadWrite,....) ValueType value, this property is the default property of the variable, that is, if you (ReadWrite and readonly are not used, Then your variable is the ReadWrite property, and by adding the ReadWrite property your variable will have a Get method, and a set method. Property (readonly,...) ValueType value, which means that the variable is only a readable method, that is, you can only use its Get method. 2,assign,setter method directly assigns value, does not perform any retain operation, in order to solve the original type and the circular reference problem 3,retain,setter method to the parameter to release the old value and then retain the new value, all implementation is 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.
In fact, they can be expressed in code: 1.nonatomic and [email protected] (nonatomic) nsobject* test1;
@synthesize test1; The above two lines of code indicate that our access to test1 is non-multithreaded security. @property (atomic) nsobject* test1;
@synthesize test1; The above two lines of code indicate that our access to test1 is multithreaded security. In fact, it is said that the member variable is placed in the mutex code, for example, the following lock. [_internal Lock]; Lock using an object-level lock
ID result = [[value retain] autorelease];
[_internal unlock];
return result;
As mentioned above, these two properties are security for access to test1 under multithreaded conditions. If your program's member variables do not have security issues, use nonatomic as well, because it is more efficient to avoid mutual exclusion in access.
2. ReadOnly, ReadWrite (note, the following process we will add the Nonatomic attribute, because it is very common) 2.1 [email protected] (nonatomic, readonly) nsobject* test1;
@synthesize test1; The above two lines of code, OBJC editor will be translated for us: @property (nonatomic, readonly) nsobject* test1; Equivalent-(nsobject*) test1;
@synthesize test1; equivalent-(nsobject*) test1{return test1;} 2.2 ReadWrite
@property (nonatomic, ReadWrite) nsobject* test1;
@synthesize test1; The above two lines of code, OBJC editor will be translated for us: @property (nonatomic, ReadWrite) nsobject* test1; Equivalent-(nsobject*) test1;

-(void) Settest1 (nsobject* Other), @synthesize test1; equivalent-(nsobject*) test1{return test1;} -(void) Settest1 (nsobject* other);
{test1 = other;}

Here to illustrate, ReadOnly, ReadWrite these two properties their true value, not provide member variable access interface, but control

The access permissions for the member variable. So grab them for real value. 3. [Email protected] (nonatomic, assign) nsobject* test1; @synthesize test1; the above two sentences: The OBJC editor will be translated as follows: @property (Nonatomic, assign) nsobject* test1; equal

-(void) Settest1 (nsobject* Other), @synthesize test1;-(void) Settest1 (nsobject* other);
{test1 = other;}
4. [Email protected] (nonatomic, retain) nsobject* test1;
@synthesize test1;

OBJC Editor translations such as:

@property (nonatomic, retain) nsobject* test1; equivalent-(nsobject*) test1;
-(void) Settest1 (nsobject* other);

@synthesize test1;

-(nsobject*) test1{return test1;}                   -(void) Settest1 (nsobject* other) {if (test1!= other) {[test1release];     test1= [Otherretain]; }}
5. [Email protected] (nonatomic, copy) nsobject* test1;
@synthesize test1;

The OBJC editor will translate as follows:

@property (nonatomic, copy) nsobject* test1;
-(nsobject*) test1;
-(void) Settest1 (nsobject* other);

@synthesize test1;

-(nsobject*) test1{return test1;} -(void) Settest1 (nsobject* other);

if (Test1!=other) {
test1= [othercopy];

For the Copy property to be primary, the object defined with the Copy property must conform to the Nscopying protocol, and you must also implement the-(ID) copywithzonenszone*) zone method.

Code is the King: are some simple code use cases//in order to be more universal, I choose to use a custom object Testobj////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////
Testobj. h
@interface testobj:nsobject{

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////
Testobj. m (ID) copywithzonenszone *) zone
testobj* POBJ = [[Testobj allocwithzone:zone] init];
return POBJ;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// TestApp is comprised of multiple Testo BJ object pointer//testapp. h
@interface TESTAPP:
testobj* test1;
testobj* _test2;

@property (nonatomic, retain) nsobject* test1;
@property (nonatomic, copy) nsobject* test2;////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////
[email protected] test1; @synthesize test2 = _test2;////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////
@synthesize test2 = _test2; To be particularly important here, this can be seen as an alias mechanism, which differs from the previous C + + header file and the CPP file.
For example, the setter function, the OBJC editor will be translated as follows if the @synthesize _test2;setter function will be this form:-(void) set_test2 (nsobject*); Note that the middle underline is written as @synthesize test = _test2;setter function will be this form:
-(void) settest2 (nsobject*);//_test2 was replaced by test2
Can see this alias mechanism, feel is normative writing (in fact, more like the specification of OBJC writing, we can look at the official document, member variables are in front with a sliding line (such as _test2), so only to engage in such an alias specification code in the writing)
Here is a function function,-(void) Test{test1 = [[testobj alloc]init];//test1 retaincount = 1;

Here are three ways to test2: _test2 = test1; Here is the pointer to the test1 to the _test2 pointer, note, and did not call Test2 setter method, so test retaincount = 1, test2 retaincount = 0;  Self.test2 = test1;   This calls Test2 's Copy method, so this is test retaincount = 1, test2 retaincount = 1; Test2 = test1; This code system will prompt an error saying that test2 is not defined. Because the compiler thinks of this as an assignment expression, test2 is considered a member variable, and there is no such member variable in our TestApp, it is necessary to distinguish what we call the alias, the alias mechanism can be considered as the call setter or Getter function will work, And here is just a simple assignment, and there are undefined errors. If you don't understand it, you'll remember to call the setter or Getter function and use the "self. Member variable" form.
We commented out the Test2 = test1 code to ensure that the program continues to execute [test1 release]; Release Test1, test1 retaincount = 0;

[Test2 release]; Release Test2, test2 retaincount = 0;
} @end

Nonatomic, Retain,weak,strong usage explanation

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: 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.