Copy and mutableCopy

Source: Internet
Author: User

Copy and mutableCopy have a better understanding of memory than a bad pen, and better understanding than writing it out. The previous sentence is very correct, and the last one is added by me. I learned what I did next but did not. The record is as follows. Variable objects and non-objects in OC are often used as follows: NSString, NSMutableString, | NSArray, NSMutableArray. The return type of the copy method of A: NSString and NSMutableString2 is as follows: [NSString copy] pointer copy: the address of the returned object is the same as that of the method receiver. The reference count is added with 1 [NSMutableString copy] for deep copy. Then, Alloc an NSString object and return it. The address of the returned object is different from the address of the method receiver. Changing the content of the method receiver does not affect the returned object. At the same time, the content of the returned object cannot be changed. Here we will talk about copy and retian of NSString. We can see that the above [NSString copy] explanation is no different from the retain's use. Why do we need two more? At that time, I was confused, and it was still different after analysis. @ Property mString; 1: If mString set is an NSMutableString copy attribute, when the set object content is modified, the mString retian attribute is not affected. When the set object content is modified, the mString content is changed. 2: If the object in mString set is NSString, whether it is the copy or retain attribute, because the object in set is NSString, the content may change, therefore, the result of using any attribute is consistent. Therefore, when selecting retain or copy for NSString, you need to consider that if the content of the set object changes, you do not need to affect yourself. If you need to, you need to retain and copy the object if you do not need it. However, we recommend that you still use copy, which is logically clear and easy to locate. The MutableCopy method of 2 returns the type [NSString mutableCopy] Deep copy, Alloc a new NSMuatbleString, and return. The address of the returned object is different from the address of the method receiver. The change of the content of the two does not affect the other, and the content of the returned object can be changed. [NSMutableString mutableCopy] Deep copy: Re-Alloc an NSMuatbleString and return. The address of the returned object is different from the address of the method receiver. The change of the content of the two does not affect the other, and the content of the returned object can be changed. B: NSArray, NSMutableArray2, the copy method returns the following type: [NSArray copy] pointer copy. The address of the returned object is the same as the address of the receiver of the method. Add 1 to the reference count, at the same time, the object addresses in the array are the same [NSMutableArray copy] "false deep copy", re-Alloc an NSArray object, and return. The returned object address is different from the method receiver address, in addition, changes to the content of the method recipient itself (operations such as Zeng, subtraction, and non-containing object content) do not affect the returned object, and the content of the returned object cannot be changed. At the same time, the object addresses in the array are the same. For example, NSArray * srcArray = [NSArray arrayWithObjects: [NSMutableString stringWithString: @ "a"], @ "B", @ "c", nil]; NSMutableArray * arrMCopy = [srcArray mutableCopy]; NSArray * arrCopy = [srcArray copy]; (lldb) p srcArray (NSArray *) $2 = 0x0cf667c0 @ "3 objects" (lldb) p arrCopy (NSArray *) $0 = 0x0cf667c0 @ "3 objects" (lldb) p arrMCopy (NSMutableArray *) $1 = 0x0d077ab0 @ "3 objects" 2 The MutableCopy method returns the type: [NSAr Ray mutableCopy] "false deep copy" re-Alloc an NSMutableArray and returns. The address of the returned object is different from the address of the method receiver, and the object of the two is increased, deletion and other operations do not affect the other. At the same time, the object addresses in the array are the same [NSMutableArray mutableCopy. NSMutableArray * srcMArray = [NSMutableArray Syntax: [NSMutableString stringWithString: @ "MArray"], @ "B", @ "c", nil]; optional * mArrMCopy = [srcMArray mutableCopy] NSArray * mArrayCopr = [srcMArray copy]; (lldb) p srcMArray (NSMutableArray *) $3 = 0x0ce70100 @ "3 objects" (lldb) p mArrMCopy (NSMutableArray *) $4 = 0x0cf667e0 @ "3 objects" (lldb) p mArrayCopr (NSArray *) $5 = 0x0cd82a50 @ "3 obje Cts. As mentioned above, whether it is NSArray or NSMutableArray, the addresses of the objects returned by their mutableCopy or copy operations are the same, that is, all objects contained in the copy operation are pointer copies. I cannot directly display the object address in the array. I can perform a test. NSString * srcObj1 = [runtime: 0]; [srcObj1 appendString: @ "IOS7"]; NSMutableString * srcObj2 = [runtime: 0]; NSString * srcObj3 = [arrCopyobjectAtIndex: 1]; (lldb) po srcArray <__nsarrayi 0xce6fdc0> (aIOS7, sss, c) (lldb) po arrCopy <__nsarrayi 0xce6fdc0> (aIOS7, sss, c) (lldb) po arrMCopy <__nsarraym 0xcf6e1e0> (aIOS7, sss, c) the object returned by NSArray's copy, as long as it modifies the content of the object that contains the object (provided that the object can be modified), mutable The content of the object returned by Copy also changes. Attaches the memory structure of the object: (lldb) p srcObj1 (NSString *) $8 = 0x0cd92020 @ "aIOS7" (lldb) p srcObj2 (NSMutableString *) $9 = 0x0cd92020 @ "aIOS7" (lldb) p * srcObj1 (NSString) $5 = {NSObject = {isa = _ NSCFString} (lldb) p * srcObj2 (NSMutableString) $6 = {NSString = {NSObject = {isa = _ NSCFString} (lldb) p * srcObj3 (NSString) $7 = {NSObject = {isa = _ NSCFString} copy and mutableCopy of NSMutableArray can be proved by yourself. Conclusion: 1: For immutable objects, copy is a light copy, that is, pointer copy. MutableCopy is returned by a new Alloc object. 2: For mutable objects, both copy and mutableCopy are returned by a new Alloc object. 3: The objects returned by copy are immutable, and the objects returned by mutableCopy are all mutable. 4: the address of the object returned by the container Class Object, whether it is mutable or immutable. The address of the object returned by copy and mutableCopy is the same as that of the previous one, that is, the objects in the container are all shortest copies.

Related Article

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.