/*****************************************
*
* 非容器类对象
*
*****************************************/
/*mstr和mstrCopy指向的是同一块内存区域,我们称之为弱引用(weak reference)。而mstrMCopy是真正的复制,
系统为其分配了新内存空间,保存从mstr复制过来的字符串值*/
NSMutableString *mstr = [NSMutableString stringWithString:@"origin"];
NSMutableString *mstrCopy = [mstr copy];
NSMutableString *mstrMCopy = [mstr mutableCopy];
NSLog(@"mstr is %@",mstr);
NSLog(@"mstrCopy is %@",mstrCopy);
NSLog(@"mstrMCopy is %@",mstrMCopy);
NSLog(@"*****************************");
/*mstrCopy它所指向的其实是一个imutable对象,是不可改变的,所以会出错*/
[mstr appendString:@"111"];
//[mstrCopy appendString:@"222"]; //Error
[mstrMCopy appendString:@"333"];
NSLog(@"mstr is %@",mstr);
NSLog(@"mstrCopy is %@",mstrCopy);
NSLog(@"mstrMCopy is %@",mstrMCopy);
NSLog(@"*****************************");
/*****************************************
*
* 容器类对象
*
*****************************************/
/*arrayCopy1是和array指向相同的对象,包括array里面的元素也是指向相同的指针*/
NSArray *array1 = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
NSArray *arrayCopy1 = [array1 copy];
NSLog(@"array1 retain count: %d",(int)[array1 retainCount]);
NSLog(@"array1 retain count: %d",(int)[arrayCopy1 retainCount]);
NSLog(@"array1 %@",array1);
NSLog(@"arrayCopy1 %@",arrayCopy1);
/*mArrayCopy1是array1的可变副本,指向的对象和array1不同,但是其中的元素和array1中的元素指向的还是同一个对象。
mArrayCopy1还可以修改自己的对象*/
NSMutableArray *mArrayCopy1 = [array1 mutableCopy];
[mArrayCopy1 addObject:@"de"];
[mArrayCopy1 removeObjectAtIndex:0];
NSLog(@"mArrayCopy1 %@",mArrayCopy1);