Deep and shallow copies in iOS

Source: Internet
Author: User

#import <Foundation/Foundation.h>intMainintargcConst Char*argv[]) {//A: The difference between copy and retain//who has the concept of reference counting://1. Heap space has a reference counting concept.//2. The heap area object will have a reference count. //%ld:-1%lu:18446744073709551615//retain: Always a shallow copy.    The reference count is added one at a time. //returns whether the object is mutable and consistent with the object being copied. //copy: For a deep copy of a mutable object, the reference count does not change; //for non-mutable objects is a shallow copy, the reference count is added one at a time. //always returns an immutable object. //NSString generally recommend that you use copy, so that changing the data does not affect the original assignment//mutablecopy: Always a deep copy, the reference count does not change. //always returns a Mutable object. //Two: The difference between a shallow copy and a deep copy//shallow copy definition: When copying the value of an object, no matter how much it is copied, the value points to the same object. //deep Copy definition: Add a pointer and request a new memory so that the added pointer points to the new memory. //a shallow copy is a pointer copy, and a deep copy is a copy of the content. //An example of a shallow copy: A person began to call Zhang San, later renamed John Doe, but still the same person. Mutual influence. //deep copy: A person began to call Zhang San, and later cloned individuals called John Doe, not the same person. Does not affect each other. //1. Of course not all objects in iOS support Copy,mutablecopy, classes that comply with the Nscopying protocol can send copy messages, and classes that follow the Nsmutablecopying protocol can send mutablecopy messages. //2. If you want to customize copy then you must obey Nscopying and implement Copywithzone: method, if you want to customize mutablecopy then you must obey Nsmutablecopying, and implement        Mutablecopywithzone: Method. //Three. iOS provides copy and Mutablecopy methods, copy is copying an immutable object, and Mutablecopy is copying a mutable object. //1. For copies of non-container class objects. (NSString, NSNumber, etc.)//for a copy of an immutable object://nsstring *str1 = [[NSString alloc] initwithformat:@ "bingbingbing"];//nsstring *str2 = [str1 copy];//nsmutablestring *STR3 = [str1 mutablecopy];//NSLog (@ "%p,%p,%p", str1, str2, STR3);//NSLog (@ "%lu,%lu,%lu", Str1.retaincount, Str2.retaincount, str3.retaincount);//[str1 release];//NSLog (@ "%p,%p,%p", str1, str2, STR3);//NSLog (@ "%lu,%lu,%lu", Str1.retaincount, Str2.retaincount, str3.retaincount);//If you copy a immutable object, copy is a shallow copy, and Mutablecopy is a deep copy. //for a copy of a mutable object://nsmutablestring *string1 = [[Nsmutablestring alloc] initwithformat:@ "Ice Ice"];//nsstring *string2 = [string1 mutablecopy];//nsmutablestring *string3 = [string1 mutablecopy];//nsmutablestring *string4 = [string1 copy];//NSLog (@ "%p,%p,%p,%p", string1, string2, String3, String4);//NSLog (@ "%lu,%lu,%lu,%lu", String1.retaincount, String2.retaincount, String3.retaincount, String4.retaincount); //if it is a copy of a mutable object, it is a deep copy, but the object returned by copy is immutable. //2. Copy of the container class object. (Nsarray, nsdictionary, etc.)//    //Nsarray *arr1 = [Nsarray arraywithobjects:@ "Bing", @ "Liang", nil];//Nsarray *arr2 = [arr1 copy];//Nsmutablearray *ARR3 = [arr1 mutablecopy];//NSLog (@ "%p,%p,%p", arr1, ARR2, ARR3);//NSLog (@ "%lu,%lu,%lu", Arr1.retaincount, Arr2.retaincount, Arr3.retaincount); //arr1 is pointing to the same object as ARR2, and the elements inside are pointing to the same pointer,//here arr2 is a shallow copy of arr1, ARR3 is a deep copy of arr1.//For (NSString *p in arr1) {//NSLog (@ "%p", p);//    }//NSLog (@ "_________________________________");//For (NSString *p in arr2) {//NSLog (@ "%p", p);//    }//NSLog (@ "_________________________________");//For (NSString *p in arr3) {//NSLog (@ "%p", p);//    }    //but the elements in the array are shallow copies.       /*nsmutablestring *mutabelstring1 = [nsmutablestring stringwithformat:@ "Bingbing"]; nsmutablestring *mutabelstring2 = [nsmutablestring stringwithformat:@ "Laingliang"];*/Nsmutablearray*array1 = [Nsmutablearray arraywithobjects:@"Bingbing",@"Laingliang", nil]; Nsarray*array2 =[array1 copy]; Nsmutablearray*array3 =[Array1 mutablecopy]; Nsmutablearray*array4 =[array1 copy]; NSLog (@"%p,%p,%p,%p", Array1, Array2, Array3, Array4); NSLog (@"%lu,%lu,%lu,%lu", Array1.retaincount, Array2.retaincount, Array3.retaincount, Array4.retaincount); //if an element of a container is immutable, the object remains unchanged after you copy it, so you only need the pointer to copy it. //For (NSString *p in array1) {//NSLog (@ "%p", p);//        }//NSLog (@ "_________________________________");//For (NSString *p in array2) {//NSLog (@ "%p", p);//        }//NSLog (@ "_________________________________");//For (NSString *p in array3) {//NSLog (@ "%p", p);//        }//NSLog (@ "_________________________________");//For (NSString *p in Array4) {//NSLog (@ "%p", p);//    }            //the difference between copy and retain://in the Foundation object, when copy is an immutable object, it acts as a retain and is a shallow copy. //when it is a mutable object, copy is a deep copy, and retain is a shallow copy. //the difference between copy and mutablecopy://in the Foundation object, copy is a shallow copy when the object is immutable. //when we use Copy a Mutable object, the deep copy, but the replica object is immutable. //when using mutablecopy, replicas are mutable regardless of whether the original object is mutable, and a deep copy is realized in real sense. //3. Custom copies of objects://The difference between a deep copy and a shallow copy is the implementation of the Copywithzone method (slightly)//A shallow copy of the copied object itself, the property in the object, the contained object does not copy//deep copy copy all, including object properties and other objects    return 0;}

Deep and shallow copies 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.