深拷貝就是生成一個(gè)新的對(duì)象捶箱,內(nèi)容和原對(duì)象完全相同店乐。淺拷貝即是指針的拷貝蹦掐,生成一個(gè)新的指針指向原對(duì)象渐溶。
//
// 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 {
/*
拷貝的意義:
在改變副本對(duì)象的時(shí)候雹锣,不會(huì)影響到原對(duì)象
定義:
深拷貝:就是拷貝出來(lái)一個(gè)新的對(duì)象己沛,內(nèi)容和原對(duì)象一樣竹祷。
淺拷貝:其實(shí)就是指針的拷貝疆拘,沒(méi)有一個(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);
// 由此可見(jiàn)附迷,即使是不可變到不可變的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)容而保持原來(lái)的對(duì)象內(nèi)容不變的話(huà),那么就用copy屬性
// 這里改變了mutableName但是stu的name屬性值沒(méi)變
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;
}
//
// 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 {
// 這里右邊需要寫(xiě)成self class這樣拨与,因?yàn)槿绻宇?lèi)調(diào)用父類(lèi)的這個(gè)方法的時(shí)候稻据,就會(huì)產(chǎn)生相應(yīng)的子類(lèi),而不是產(chǎn)生父類(lèi)
// 左邊不用動(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
//
// 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)用父類(lèi)的方法
GoodStudent *copy = [super copyWithZone:zone];
copy.age = self.age;
return copy;
}
@end