iOS Development Knowledge Points: Understanding Assign,copy,retain to Strong

Source: Internet
Author: User

Description of changes to. h and. m files

1. For. h header files, the main is to change the attribute definition from retain to strong

[Java]View Plaincopy
    1. @property (retain, nonatomic)

Into

[Java]View Plaincopy
    1. @property (Strong, Nonatomic)


2. Before arc, we often use category extensions in. m to increase the private property

[Java]View Plaincopy
    1. @interface Mjviewcontroller ()
    2. @property (nonatomic, retain) Nsarray *data;
    3. @end

This is done primarily by simplifying the manual memory management of the instance object, allowing the property setter method to automatically manage the release of the original object and the retain of the new object. But with arc, this code is no longer needed. In general, it is no longer necessary to use the property just to simplify memory management, although you can still do it, but using instance variables directly is a better choice. Only instance variables that belong to public should be defined as property

We can define private instance variables directly in the. M class implementation without having to write the classification extension:

[Java]View Plaincopy
    1. @implementation Mjviewcontroller {
    2. Nsarray *data;
    3. }

However, it is still necessary to set data to nil in the Viewdidunload method because data is a strong pointer and should be set to nil when an object is no longer used

[Java]View Plaincopy
    1. -(void) Viewdidunload {
    2. [Super Viewdidunload];
    3. data = nil;
    4. }


Second, Iboutlet

In Arc, all Iboutlet properties are recommended to use weak, which are already part of view controller's view hierarchy and do not need to be defined again as strong. Therefore, these iboutlet properties that are defined as weak do not need to be set to nil in Iboutlet


Three, @property modifier summary

Strong: The property value corresponds to the __strong keyword, that is, the variable declared by the attribute becomes the holder of the object, equivalent to "retain"
Weak: This property corresponds to the __weak keyword, which is consistent with the variable defined by the __weak, the variable declared by the property will not have ownership of the object, and when the object is freed, the object will be automatically assigned nil, remembering that Iboutlet should use weak
unsafe_unretained: A variable equivalent to the __unsafe_unretaind keyword declaration, equivalent to the previous "assign", used by systems prior to IOS 5 to use this attribute instead of weak
copy: Copy an object and create a strong association like the previous copy
Assign: The object cannot use assign, but the original type (BOOL, int, float) can still be used


As an example:

NSString *houseofmm = [[nsstring alloc] initwithstring: ' three rooms and two halls ' of Van Gogh ';

The preceding section of code performs the following two actions:
1 Allocate a piece of memory on the heap to store @ 'The three-room, two-hall ', for example: memory address0x1111 content is 'The three-room and two halls of Van Gogh
2 Allocate a section of memory on the stack to storeHouseforwife, for example: address is0XAAAA content is naturally0x1111


See below (AssignRetaincopy):
1.assign of the situation:NSString *Myhouse = [HouseofmmAssign];
At this timeMyhouse andFoSEOFMM exactly the same, the addresses are all0XAAAA, Content is0x1111, i.e.Myhouse justAn alias for HOUSEOFMM, which is equal to another operation on any one operation. SoRetaincount does not need to be added. (The same in the same, the relationship is good, a key, give me Hold)
2.retain of the situation:nsstring  *  myhouse  = [HOUSEOFMM   retain]; 
at this time myhouse address is no longer Span style= "color: #e56600;" >0XAAAA, may be 0xaabb, but the content remains 0x1111. So myhouse and HOUSEOFMM can manage the '

At this time will be on the heap to re-open a memory storage @ ' in the three-room two halls, such as 0x1122, the content is @ ' Van Gogh's three rooms and two halls ', at the same time will be on the stack for Myhouse allocated space, such as address:0XAACC, content for 0x1122, so Retaincount added 1 for Myhouse to manage the memory of 0x1122. Two sets @ ' the three-room two hall of Van Gogh ', the condition is good, separated, the house one person, so the key is a person. )



When do you useAssign, of course, was a broken house, a paperback house to pull
Base type (simple Type, atomic type):Nsinteger,Cgpoint,Cgfloat,c Data Type (int,float,double,< Span style= "color: #9933e5;" >char, etc.)


When to use copy
contains classes of mutable subclasses that can be deeply copied, such as nsarray,nsset, Nsdictionary,nsdata, nscharacterset,nsindexset,nsstring
(house that can be copied in depth)
but Nsmutablearray can't, mutable can't use copy, Otherwise the initialization will be problematic. Remember when

uses retain
Other nsobject and its subclass objects good (most)


strong equals retain, After the arc came, I got a key strong.

Copy: http://wenku.baidu.com/view/ab6ed804bed5b9f3f90f1c7d.html


If you have time, please translate it well:


Http://clang.llvm.org/docs/AutomaticReferenceCounting.html


https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/chapters/ocproperties.html


http://developer.apple.com/library/ios/#releasenotes/objectivec/rn-transitioningtoarc/introduction/introduction.html


First look at this:

http://blog.csdn.net/favormm/article/details/7023322

http://www.crifan.com/object_c_property_setter_assign_retain_copy_weak_strong/

http://www.crifan.com/object_c_property_setter_assign_retain_copy_weak_strong/

iOS Development Knowledge Points: Understanding Assign,copy,retain to Strong

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.