"Learning the path to iOS: Objective-c" deep copy and shallow copy

Source: Internet
Author: User

The difference between shades of a copy:

Deep Copy Copy Content

Shallow copy copy Address

Note: When an object is str1 in a constant area

① copy NSString object to immutable string

        NSString *str1 = @ "Hello"; STR1--Constant Zone        NSLog (@ "%p", str1);//0x100002048        //copy        nsstring *str2 = [str1 copy];//0x100002048 str2--Constant area        NSLog (@ "%p", str2),//STR2 and str1 all point to constant area, same address, reference count is Infinity        NSLog (@ "str2 Retaincount =%lu", [str2 Retaincount]); <span style= "font-size:18px;" >//18446744073709551615</span>
② NSString objects for immutable strings Mutablecopy

        Mutablecopy, deep Copy, copy content, and copy it is a variable object        nsmutablestring *str3 = [str1 mutablecopy];        NSLog (@ "Str3 Retaincount =%lu", [Str3 Retaincount]);//1        [Str3 appendstring:@ "World"];//operations on copied objects        NSLog (@ "STR3 =%@", STR3);
Note: When the object str1 in the heap areawhen

        NSString *str1 = [NSString stringwithformat:@ "Frank"];        NSLog (@ "str1 =%p, Retaincount =%lu", str1, [str1 Retaincount]);//1
① copy NSString object to immutable string

        Copy, shallow copy, copy address, and add 1 to the reference count of the original object.        NSString *STR2 = [str1 copy];//str2 and str1 point to the same heap area space        NSLog (@ "str2 =%p, Retaincount =%lu", str2, [str2 Retaincount]); /2
② NSString objects for immutable strings mutablecopy
        Mutablecopy, deep Copy, copy content, the heap area opens up new space, and the copied object is mutable.        nsmutablestring *STR3 = [str1 mutablecopy];//str3 and str1 point to different objects        [str3 appendstring:@ "Lanou"];        NSLog (@ "STR3 =%p, Retaincount =%lu", STR3, [Str3 Retaincount]);//1
variable string (nsmutableString)
① to a mutable string nsmutablestring object copy
        The original object str1 in the heap area        nsmutablestring *str1 = [[Nsmutablestring alloc] initwithformat:@ "Frank"];        NSLog (@ "str1 =%p", str1),//0x10010b800        //copy A new space in the heap, copying the contents to a new space, but copying the immutable string object.        NSString *STR2 = [str1 copy];        NSLog (@ "str2 =%p, Retaincount =%lu", str2, [str2 Retaincount]);//1
② nsmutablestring object to a mutable string mutablecopy

        Mutablecopy A new space in the heap, copying the content to a new space, but copying a mutable string object.        nsmutablestring *STR3 = [str1 mutablecopy];        [Str3 appendstring:@ "Lanou"];        NSLog (@ "STR3 =%p, Retaincount =%lu", STR3, [Str3 Retaincount]);
Summary:

        /**         *  Summary: The copy is divided into mutable objects and immutable objects.            A shallow copy---  only copies the object address, does not copy the content, does not open up the new space.            Deep copy---  copy content, and the heap area opens up new space.            Immutable object: 1.copy is equivalent to Retin, the original object reference count plus 1, copy the address.                     2.mutableCopy Copy the new object, and the object is variable, open the content.            Mutable object: 1.copy Copies the new object, but the object is immutable and copies the contents.                     2.mutableCopy copies the new object, but the object is mutable and copies the contents.         */
③. For a custom object copy and Mutablecopy, you need to obey <nscopying, nsmutablecopying> protocol, for example:

1. Create Person.h

@interface person:nsobject <nscopying, nsmutablecopying> @property (nonatomic, copy) NSString *name; Name @property (nonatomic, copy) NSString *gender; Sex @property (nonatomic) Nsinteger age;//age @end
2.person.m File ComplianceProtocol
@implementation person-(ID) Copywithzone: (Nszone *) zone{person    *newperson = [[self class] allocwithzone:zone] INIT];    Newperson.name = [self.name copy];    Newperson.gender = [Self.gender copy];    Newperson.age = Self.age;    return Newperson;} -(ID) Mutablecopywithzone: (Nszone *) zone{person    *newperson = [[self class] allocwithzone:zone] init];    Newperson.name = [Self.name mutablecopy];    Newperson.gender = [Self.gender mutablecopy];    Newperson.age = Self.age;    return Newperson;} -(void) dealloc{    self.name = nil;    Self.gender = nil;    [Super Dealloc];} @end
implementation, analysis of deep-and-shallow copies

       Person copy with mutablecopy nsstring *name = [NSString stringwithformat:@ "Wang"];                NSString *gender = [NSString stringwithformat:@ "man"];        Person *per = [[Person alloc] init];        Per.name = name;        Per.gender = gender; NSLog (@ "per =%p", per);//0x10010b980//per name = 0x100100560, gender = 0X100104AC0 (pre-copy address)  nslo                G (@ "per name =%p, gender =%p", Per.name, Per.gender);                Person *copyperson = [per copy];                                      Copyperson = 0x100500a90, Retaincount = 1 (post-copy address)  nslog (@ "Copyperson =%p, Retaincount =%lu",        Copyperson, [Copyperson Retaincount]); (Copy the content address unchanged, copy the address in the past)//copyperson name = 0x100100560, gender = 0x100104ac0 NSLog (@ "Copyperson name =%p,            Gender =%p ",  copyperson.name, Copyperson.gender);               Person *mutacopyperson = [per Mutablecopy]; CopymutacopypErsonperson = 0X100401D50, Retaincount = 1  nslog (@ "Copymutacopypersonperson =%p, Retaincount         =%lu ", Mutacopyperson, [Mutacopyperson Retaincount]); (mutablecopy) address is different, that is, the copy is the content//mutacopyperson name = 0x100401450, gender = 0x1004014e0 NSLog (@ "Mutacopyperson         Name =%p, gender =%p ",  mutacopyperson.name, Mutacopyperson.gender);



"Learning the path to iOS: Objective-c" deep copy and shallow copy

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.