深拷貝就是生成一個新的對象,內(nèi)容和原對象完全相同。淺拷貝即是指針的拷貝,生成一個新的指針指向原對象。
//
// main.m
// 深拷貝與淺拷貝
//
// 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 {
/*
拷貝的意義:
在改變副本對象的時候,不會影響到原對象
定義:
深拷貝:就是拷貝出來一個新的對象,內(nèi)容和原對象一樣。
淺拷貝:其實就是指針的拷貝,沒有一個新的對象生成,只是多了一個指向原對象的指針,將原對象的指針賦值給了新聲明的指針,即,是一個指針的拷貝。
*/
// 只有從不可變對象copy到不可變對象的時候才是淺拷貝,其他的都是深拷貝
NSString *string = @"abcde";
NSString *str = [string copy];
NSLog(@"string: %@", string);
NSLog(@"str: %@", str);
// 因為指向同一個對象,所以原對象的引用計數(shù)+1
NSLog(@"string和str是否是同一個對象: %d", string == str);
// 由可變字符到不可變字符的copy是深拷貝
NSMutableString *mutableString = [NSMutableString stringWithFormat:@"abc%@", @"dddddd"];
NSString *str1 = [mutableString copy];
NSLog(@"mutableString和str1是否是同一個對象: %d", mutableString == str1);
// mutalbeCopy都是深拷貝
NSString *str2 = @"abbbbbbb";
NSMutableString *mutableString1 = [str2 mutableCopy];
NSLog(@"str2和mutableString1是否是同一個對象: %d", str2 == mutableString1);
// 由此可見,即使是不可變到不可變的mutableCopy也是深拷貝,進一步證明了mutableCopy是深拷貝
NSString *str3 = @"kkkkkkk";
NSString *string1 = [str3 mutableCopy];
NSLog(@"str3和string1是否是同一個對象: %d", str3 == string1);
NSMutableString *myStr1 = [NSMutableString stringWithFormat:@"abc%d", 10];
NSMutableString *myStr2 = [myStr1 copy];
// 這里的myStr2實際是一個immutable對象,是不可變的,所以改變myStr2對象會報錯
// [myStr2 appendString:@"fdsaf"];
NSLog(@"myStr1: %@", myStr1);
NSLog(@"myStr2: %@", myStr2);
NSLog(@"myStr1和myStr2是否是同一個對象: %d", myStr1 == myStr2);
// 如果有改變副本的內(nèi)容而保持原來的對象內(nèi)容不變的話,那么就用copy屬性
// 這里改變了mutableName但是stu的name屬性值沒變
Student *stu = [[Student alloc] 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和mutableName是否是同一個對象: %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
// 深拷貝與淺拷貝
//
// 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
// 深拷貝與淺拷貝
//
// Created by on 15/4/10.
// Copyright (c) 2015年 apple. All rights reserved.
//
#import "Student.h"
@implementation Student
+ (id)studentWithName:(NSString *)name {
// 這里右邊需要寫成self class這樣,因為如果子類調(diào)用父類的這個方法的時候,就會產(chǎn)生相應的子類,而不是產(chǎn)生父類
// 左邊不用動,這表現(xiàn)了面向?qū)ο蟮奶攸c,多態(tài)
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
// 深拷貝與淺拷貝
//
// 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
// 深拷貝與淺拷貝
//
// 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 {
// 一定要調(diào)用父類的方法
GoodStudent *copy = [super copyWithZone:zone];
copy.age = self.age;
return copy;
}
@end