iOS learning-deep copy and shallow copy

Source: Internet
Author: User

main.m//deep copy with light copy////Created by on 15/4/10.//Copyright (c) 2015 Apple. All rights reserved.//#import <Foundation/Foundation.h> #import "Student.h" #import "GoodStudent.h" int main (int                     ARGC, const char * argv[]) {@autoreleasepool {/* * copy Meaning: The original object will not be affected when changing the Copy object                Definition: Deep copy: A new object is copied, and the content is the same as the original object.                  Shallow copy: is actually a copy of the pointer, not a new object generation, just a pointer to the original object, the original object pointer is assigned to the new declaration of the pointer, that is, a copy of the pointer.        *////Only the light copy is a non-mutable object copy to the immutable object, the others are deep copy nsstring *string = @ "ABCDE";        NSString *STR = [string copy];        NSLog (@ "string:%@", string);        NSLog (@ "str:%@", str);        Because it points to the same object, the reference count of the original object is +1 NSLog (@ "string and str are the same object:%d", string = = str); Copy by variable character to immutable character is a deep copy of nsmutablestring *mutablestring = [nsmutablestring stringwithformat:@ "abc%@", @ "ddddd        D "];        NSString *STR1 = [mutablestring copy]; NSLog (@ "mutablestring and str1 are the same object:%d", mutablestring = = str1);        Mutalbecopy are deep copies of nsstring *STR2 = @ "ABBBBBBB";        nsmutablestring *mutablestring1 = [str2 mutablecopy]; NSLog (@ "str2 and mutableString1 are the same object:%d", str2 = = mutableString1);//This shows that even the immutable mutablecopy is a deep copy, further proving mutable        Copy is a deep copy of nsstring *STR3 = @ "KKKKKKK";        NSString *string1 = [Str3 mutablecopy];                        NSLog (@ "STR3 and string1 are the same object:%d", str3 = = string1);        nsmutablestring *mystr1 = [nsmutablestring stringwithformat:@ "abc%d", 10];        nsmutablestring *MYSTR2 = [myStr1 copy];        Here the MYSTR2 is actually a immutable object, is immutable, so change the MyStr2 object will be error//[MYSTR2 appendstring:@ "Fdsaf"];        NSLog (@ "MYSTR1:%@", MYSTR1);        NSLog (@ "MYSTR2:%@", MYSTR2);                NSLog (@ "myStr1 and MYSTR2 are the same object:%d", myStr1 = = MYSTR2); If you change the contents of the copy and keep the original object content unchanged, then use the Copy property//Here to change the mutablename but the Stu Name property value does not change Student *stu = [[Student All        OC] init]; NsmutablesTring *mutablename = [nsmutablestring stringwithformat:@ "aaaaaa%d", 10];        Stu.name = Mutablename;        [Mutablename appendstring:@ "Fdsafdsafsad"];        NSLog (@ "Stu.name:%@", stu.name);        NSLog (@ "Mutablename:%@", mutablename);                                        NSLog (@ "Stu.name and Mutablename are the same object:%d", stu.name = = Mutablename);        Student *STU1 = [Student studentwithname:@ "STU1"];        Student *STU2 = [stu1 copy];        NSLog (@ "%@", stu1.name);                NSLog (@ "%@", stu2.name);        Stu2.name = @ "STU2";        NSLog (@ "%@", stu1.name);                                        NSLog (@ "%@", stu2.name);        Goodstudent *goodstudent1 = [goodstudent goodstudentwithage:10 name:@ "Jack"];        Goodstudent *goodstudent2 = [goodStudent1 copy];        Goodstudent2.name = @ "Tom";        Goodstudent2.age = 20;        NSLog (@ "goodStudent1, Name:%@, Age:%ld", Goodstudent1.name, Goodstudent1.age); NSLog (@ "GoodStudent2, Name:%@, Age:%ld", goodStudent2.name, Goodstudent2.age); } return 0;}
  student.h//  deep copy with light copy////  Created by on 15/4/10.//  Copyright (c) 2015 Apple. All rights reserved.//#import <Foundation/Foundation.h> @interface student:nsobject <NSCopying> @property (nonatomic, copy) NSString *name;+ (ID) studentwithname: (NSString *) name; @end
  student.m//  deep copy with light copy////  Created by on 15/4/10.//  Copyright (c) 2015 Apple. All rights reserved.//#import "Student.h" @implementation student+ (ID) studentwithname: (NSString *) name {//    The right side needs to be written to the self class, because if the subclass calls this method of the parent class, it will produce the corresponding subclass instead of the parent class//    left without moving, which shows the object-oriented feature, polymorphic    Student *stu = [[Self Class] alloc] init];    Stu.name = name;    return Stu;} -(ID) Copywithzone: (Nszone *) zone {    Student *copy = [[[Self class] allocwithzone:zone] init];    Copy.name = Self.name;    return copy;} @end
  goodstudent.h//  deep copy with light copy////  Created by on 15/4/10.//  Copyright (c) 2015 Apple. All rights reserved.//#import <Foundation/Foundation.h> #import "Student.h" @interface goodstudent: Student@property (nonatomic, assign) Nsinteger age;+ (ID) goodstudentwithage: (int) Age Name: (NSString *) name; @end
  goodstudent.m//  deep copy with light copy////  Created by on 15/4/10.//  Copyright (c) 2015 Apple. All rights reserved.//#import "GoodStudent.h" @implementation goodstudent+ (ID) goodstudentwithage: (int.) Age Name: ( NSString *) name {    Goodstudent *good = [Goodstudent studentwithname:name];        Good.age = age;        return good;} -(ID) Copywithzone: (Nszone *) zone {    //must call the method of the parent class    goodstudent *copy = [Super Copywithzone:zone];        Copy.age = Self.age;        return copy;} @end

iOS learning-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.