TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
继续上一讲的浅拷贝,深拷贝的区别在于是否拷贝对象中的引用对象1、创建一个数组array,将类Car创建三个对象并保存到数组array中,使用[[NSMutableArray alloc] initWithArray:array]方法将array数组拷贝到array1中,修改array中的car元素,查看array1中的car是否发生变化;
2、使用[[NSMutableArray alloc] initWithArray:array copyItems:YES]方法将array拷贝到array2中,修改array中的car元素,查看array2中的car是否发生变化;
SACar.h- //
- // SACar.h
- // DeepCopy
- //
- // Created by yusian on 14-1-22.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface SACar : NSObject <NSCopying>
- @property (assign) NSString * name;
- @property int year;
- @end
复制代码 SACar.m- //
- // SACar.m
- // DeepCopy
- //
- // Created by yusian on 14-1-22.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import "SACar.h"
- @implementation SACar
- //***************************************NSCopying
- - (id) copyWithZone:(NSZone *)zone {
- SACar * c = [[[[self class] allocWithZone:zone] init] autorelease];
- c.name = self.name;
- c.year = self.year;
- return c;
- }
- //***************************************NSCopying_end
- - (NSString *) description {
- return [NSString stringWithFormat:@"%@ age is %d", self.name, self.year];
- }
- - (void) dealloc {
- NSLog(@"%@ is dealloc", self.name);
- self.name = nil;
- [super dealloc];
- }
- @end
复制代码 main.m- //
- // main.m
- // DeepCopy
- //
- // Created by yusian on 14-1-22.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "SACar.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- NSMutableArray * array = [[NSMutableArray alloc] init];
-
- for (int i = 0; i < 3; i ++) {
- SACar * car = [[SACar alloc] init];
- car.name = [NSString stringWithFormat:@"Car%d", i];
- car.year = 2000 + i;
- [array addObject:car];
- [car release];
- }
- NSLog(@"%@", array);
-
- NSMutableArray * array1 = [[NSMutableArray alloc] initWithArray:array];
- [[array1 objectAtIndex:0] setName:@"NewCar"];
- NSLog(@"%@", array);
-
- NSMutableArray * array2 = [[NSMutableArray alloc] initWithArray:array copyItems:YES];
- [[array2 objectAtIndex:0] setName:@"SecondCar"];
- NSLog(@"%@", array);
- NSLog(@"%@", array2);
-
- // [array release];
- // [array1 release];
- // [array2 release];
- }
- return 0;
- }
复制代码 运行结果:- 2014-01-22 21:40:09.327 DeepCopy[2643:303] (
- "Car0 age is 2000",
- "Car1 age is 2001",
- "Car2 age is 2002"
- )
- 2014-01-22 21:40:09.329 DeepCopy[2643:303] (
- "NewCar age is 2000",
- "Car1 age is 2001",
- "Car2 age is 2002"
- )
- 2014-01-22 21:40:09.329 DeepCopy[2643:303] (
- "NewCar age is 2000",
- "Car1 age is 2001",
- "Car2 age is 2002"
- )
- 2014-01-22 21:40:09.329 DeepCopy[2643:303] (
- "SecondCar age is 2000",
- "Car1 age is 2001",
- "Car2 age is 2002"
- )
- 2014-01-22 21:40:09.330 DeepCopy[2643:303] Car2 is dealloc
- 2014-01-22 21:40:09.330 DeepCopy[2643:303] Car1 is dealloc
- 2014-01-22 21:40:09.330 DeepCopy[2643:303] SecondCar is dealloc
- Program ended with exit code: 0
复制代码 结果表示:
进行浅拷贝后,修改array中的Car0为“NewCar”后,array与array1中的Car0都变成了NewCar;
进行深拷贝后,修改array2中的Car0为“SecondCar”后,array中的Car0依然为之前的NewCar;
array2打印出来的结果中,Car0被修改成SecondCar,并且array2被自动释放,array与array1却没有,为什么?
|
|