标签:style blog http color io os ar 使用 sp
retain属性表示两个对象地址相同(建立一个指针,指针拷贝),内容当然相同,这个对象的retain值+1也就是说,retain 是指针拷贝,copy 是内容拷贝。
当然在ios中并不是所有的对象都支持copy,mutableCopy,遵守NSCopying 协议的类可以发送copy消息,遵守NSMutableCopying 协议的类才可以发送mutableCopy消息。假如发送了一个没有遵守上诉两协议而发送 copy或者 mutableCopy,那么就会发生异常。但是默认的ios类并没有遵守这两个协议。如果想自定义一下copy 那么就必须遵守NSCopying,并且实现 copyWithZone: 方法,如果想自定义一下mutableCopy 那么就必须遵守NSMutableCopying,并且实现 mutableCopyWithZone: 方法。
首先我们需要有这样的一个前提:[array addObject:obj];
id temp = [array objectAtIndex:0]; [array removeObjectAtIndex:0];
NSString *string = @"origion"; NSString *stringCopy = [string copy]; NSMutableString *stringMCopy = [string mutableCopy]; [stringMCopy appendString:@"!!"];
NSMutableString *string = [NSMutableString stringWithString: @"origion"]; NSString *stringCopy = [string copy]; NSMutableString *mStringCopy = [string copy]; NSMutableString *stringMCopy = [string mutableCopy]; [mStringCopy appendString:@"mm"];//error [string appendString:@" origion!"]; [stringMCopy appendString:@"!!"];
//copy返回不可变对象,mutablecopy返回可变对象 NSArray *array1 = [NSArray arrayWithObjects:@"a",@"b",@"c",nil]; NSArray *arrayCopy1 = [array1 copy]; //arrayCopy1是和array同一个NSArray对象(指向相同的对象),包括array里面的元素也是指向相同的指针 NSLog(@"array1 retain count: %d",[array1 retainCount]); NSLog(@"array1 retain count: %d",[arrayCopy1 retainCount]); NSMutableArray *mArrayCopy1 = [array1 mutableCopy]; //mArrayCopy1是array1的可变副本,指向的对象和array1不同,但是其中的元素和array1中的元素指向的是同一个对象。mArrayCopy1还可以修改自己的对象 [mArrayCopy1 addObject:@"de"]; [mArrayCopy1 removeObjectAtIndex:0];
NSArray *mArray1 = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"a"],@"b",@"c",nil]; NSArray *mArrayCopy2 = [mArray1 copy]; NSLog(@"mArray1 retain count: %d",[mArray1 retainCount]); NSMutableArray *mArrayMCopy1 = [mArray1 mutableCopy]; NSLog(@"mArray1 retain count: %d",[mArray1 retainCount]); //mArrayCopy2,mArrayMCopy1和mArray1指向的都是不一样的对象,但是其中的元素都是一样的对象——同一个指针 //一下做测试 NSMutableString *testString = [mArray1 objectAtIndex:0]; //testString = @"1a1";//这样会改变testString的指针,其实是将@“1a1”临时对象赋给了testString [testString appendString:@" tail"];//这样以上三个数组的首元素都被改变了
NSArray *array = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"first"],[NSStringstringWithString:@"b"],@"c",
nil]; NSArray *deepCopyArray=[[NSArray alloc] initWithArray: array copyItems: YES]; NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData: [NSKeyedArchiver archivedDataWithRootObject: array]];
@interface MyObj : NSObject<NSCopying,NSMutableCopying> { NSMutableString *name; NSString *imutableStr; int age; } @property (nonatomic, retain) NSMutableString *name; @property (nonatomic, retain) NSString *imutableStr; @property (nonatomic) int age; @end @implementation MyObj @synthesize name; @synthesize age; @synthesize imutableStr; - (id)init { if (self = [super init]) { self.name = [[NSMutableString alloc]init]; self.imutableStr = [[NSString alloc]init]; age = -1; } return self; } - (void)dealloc { [name release]; [imutableStr release]; [super dealloc]; } - (id)copyWithZone:(NSZone *)zone { MyObj *copy = [[[self class] allocWithZone:zone] init]; copy->name = [name copy]; copy->imutableStr = [imutableStr copy]; // copy->name = [name copyWithZone:zone];; // copy->imutableStr = [name copyWithZone:zone];// copy->age = age; return copy; } - (id)mutableCopyWithZone:(NSZone *)zone { MyObj *copy = NSCopyObject(self, 0, zone); copy->name = [self.name mutableCopy]; copy->age = age; return copy; }
标签:style blog http color io os ar 使用 sp
原文地址:http://www.cnblogs.com/LJson/p/4033097.html