深拷貝就是生成一個(gè)新的對(duì)象,內(nèi)容和原對(duì)象完全相同。淺拷貝即是指針的拷貝,生成一個(gè)新的指針指向原對(duì)象
#import <Foundation/Foundation.h>
#import "Student.h"
#import "GoodStudent.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
拷貝的意義:
在改變副本對(duì)象的時(shí)候三圆,不會(huì)影響到原對(duì)象
定義:
深拷貝:就是拷貝出來一個(gè)新的對(duì)象,內(nèi)容和原對(duì)象一樣避咆。
淺拷貝:其實(shí)就是指針的拷貝舟肉,沒有一個(gè)新的對(duì)象生成,只是多了一個(gè)指向原對(duì)象的指針查库,將原對(duì)象的指針賦值給了新聲明的指針路媚,即,是一個(gè)指針的拷貝樊销。
*/
// 只有從不可變對(duì)象copy到不可變對(duì)象的時(shí)候才是淺拷貝整慎,其他的都是深拷貝
NSString *string = @"abcde";
NSString *str = [string copy];
NSLog(@"string: %@", string);
NSLog(@"str: %@", str);
// 因?yàn)橹赶蛲粋€(gè)對(duì)象,所以原對(duì)象的引用計(jì)數(shù)+1
NSLog(@"string和str是否是同一個(gè)對(duì)象: %d", string == str);
// 由可變字符到不可變字符的copy是深拷貝
NSMutableString *mutableString = [NSMutableString stringWithFormat:@"abc%@", @"dddddd"];
NSString *str1 = [mutableString copy];
NSLog(@"mutableString和str1是否是同一個(gè)對(duì)象: %d", mutableString == str1);
// mutalbeCopy都是深拷貝
NSString *str2 = @"abbbbbbb";
NSMutableString *mutableString1 = [str2 mutableCopy];
NSLog(@"str2和mutableString1是否是同一個(gè)對(duì)象: %d", str2 == mutableString1);
// 由此可見围苫,即使是不可變到不可變的mutableCopy也是深拷貝裤园,進(jìn)一步證明了mutableCopy是深拷貝
NSString *str3 = @"kkkkkkk";
NSString *string1 = [str3 mutableCopy];
NSLog(@"str3和string1是否是同一個(gè)對(duì)象: %d", str3 == string1);
NSMutableString *myStr1 = [NSMutableString stringWithFormat:@"abc%d", 10];
NSMutableString *myStr2 = [myStr1 copy];
// 這里的myStr2實(shí)際是一個(gè)immutable對(duì)象,是不可變的剂府,所以改變myStr2對(duì)象會(huì)報(bào)錯(cuò)
// [myStr2 appendString:@"fdsaf"];
NSLog(@"myStr1: %@", myStr1);
NSLog(@"myStr2: %@", myStr2);
NSLog(@"myStr1和myStr2是否是同一個(gè)對(duì)象: %d", myStr1 == myStr2);
// 如果有改變副本的內(nèi)容而保持原來的對(duì)象內(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是否是同一個(gè)對(duì)象: %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;
}
#import <Foundation/Foundation.h>
@interface Student : NSObject <NSCopying>
@property (nonatomic, copy) NSString *name;
+ (id)studentWithName:(NSString *)name;
@end
#import "Student.h"
@implementation Student
+ (id)studentWithName:(NSString *)name {
// 這里右邊需要寫成self class這樣,因?yàn)槿绻宇愓{(diào)用父類的這個(gè)方法的時(shí)候腺占,就會(huì)產(chǎn)生相應(yīng)的子類淤袜,而不是產(chǎn)生父類
// 左邊不用動(dòng),這表現(xiàn)了面向?qū)ο蟮奶攸c(diǎn)衰伯,多態(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
#import <Foundation/Foundation.h>
#import "Student.h"
@interface GoodStudent : Student
@property (nonatomic, assign) NSInteger age;
+ (id)goodStudentWithAge:(int)age name:(NSString *)name;
@end
#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