Meaning of assign, copy, retain and other keywords in ios

Source: Internet
Author: User

Assign: Simple assignment, do not change index count copy: Creates an object with an index count of 1, and then releases the old object retain: Frees the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1Copy in fact, it is the same object that was created. And retain is not: for example a NSString object, the address is 0x1111, the content is @ "STR" after the copy to another nsstring, the address is 0x2222, the content is the same, the new object retain is 1, the old object has not changed retain to another nsstring after the same address (set up a pointer, pointer copy), the content of course, the retain value of this object+1that is, retain is a pointer copy and copy is a copy of the content. The old object will be released before the copy. * Use assign: For the underlying data type (Nsinteger) and the C data type (int,float,Double,Char, etc.)*using copy: on NSString*use retain: For other nsobject and its subclasses1the. ReadOnly indicates that this property is read-only, that is, only the getter method is generated, and the setter method is not generated. 2. ReadWrite, setting the available access level3. Retain, is the value of the property at the time of assignment, first release before the values, and then assign a new value to the property, the reference plus 1. 4. 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. The difference between retain and copy and assign.1Suppose you allocate a chunk of memory with malloc and assign its address to pointer a, and then you want pointer b to share the memory, so you assign a to (assign) B. At this time A and B point to the same piece of memory, I ask when a no longer need this memory, can you directly release it? The answer is no, because a does not know whether B is still using this memory, and if A is released, then B will cause the program to crash when it uses this memory. 2To learn about the assign problem in 1, how to solve it? The simplest method is to use a reference count (reference counting), or the above example, we set a reference count for that memory, and when the memory is assigned and assigned to a, the reference count is 1. The reference count increases to 2 when a is assigned to B. If a no longer uses this memory, it only needs to subtract the reference count by 1, indicating that it no longer owns the memory. b The reference count is also reduced by 1 when the memory is no longer used. When the reference count becomes 0, the memory is no longer referenced by any pointers, and the system can release it directly. 3the above two point is actually the difference between assign and retain, assign is the direct assignment, which may cause 1 problems, when the data is int, float and other native types, you can use assign. Retain as described in 2, using a reference count, retain causes a reference count plus 1, release causes a reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called and memory is recycled. 4. Copy is used when you don't want a and b to share a piece of memory. A and B each have their own memory. 5the. Atomic and nonatomic are used to determine whether the compiler-generated getter and setter are atomic operations. In a multithreaded environment, atomic operations are necessary, otherwise they may cause incorrect results. Adding the Atomic,setter function will change to the following:if(Property! =newvalue) {[Property release];p Roperty=[newvalue retain];} About retain,copy,assign The difference problem actually troubled me for a long time, because in the program is not too often used to copy,assign, so the specific differences of the three have not quite understood. According to my understanding, assign and retain difference, is introduced a counter retaincount, can be a memory of the release of a lot of convenience. Copy, is to copy the original memory again, so that each has a memory, so that the release of the time will not be wrong. Assign: Simple assignment, without changing the index count (Reference counting). Copy: Create an object with an index count of 1 and then release the old object retain: Frees the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1 using assign: For the underlying data type (nsinteger,cgfloat) and the C data type ( int,float,Double,Char, and so on) use copy: Use retain for NSString: Nonatomic for other nsobject and its subclasses, 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 @property (Nonatomic, retain) Uitextfield*username code generated automatically at compile time-(Uitextfield *) UserName {returnUserName;}- (void) Setusername: (Uitextfield *) username_ {[UserName release];username=[username_ retain];} @property (retain) Uitextfield*Username Auto-generated code-(Uitextfield *) userName {Uitextfield*retval =Nil; @synchronized (self) {retval=[[UserName retain] autorelease];}returnretval;}- (void) Setusername: (Uitextfield *) username_ {@synchronized (self) {[UserName release];username=[username_ retain];}}

Meaning of assign, copy, retain and other keywords in ios

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.