@property (nonatomic, strong) NSString *para1;
@property (nonatomic, copy) NSString *para2;
@property (nonatomic, strong) NSMutableString *para3;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.para1 = @"meng";
self.para2 = @"zq";
self.para3 = [[NSMutableString alloc] initWithString:@"meng123"];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:self.para1, self.para2, nil];
[array addObject:self.para3];
//para1的指针地址,para1对象的地址
NSLog(@"%p, %p", &_para1, self.para1);
//para2的指针地址,para2对象的地址
NSLog(@"%p, %p", &_para2, self.para2);
NSLog(@"%p, %p", &_para3, self.para3);
//与para1、para2对象的地址相同,说明是浅拷贝
NSLog(@"%p, %p, %p", [array objectAtIndex:0], [array objectAtIndex:1], [array objectAtIndex:2]);
self.para1 = @"";
self.para2 = @"";
[self.para3 appendString:@"456"];
//与para1、para2对象的地址相同,说明是浅拷贝
NSLog(@"****%p, %p****, ****%p", [array objectAtIndex:0], [array objectAtIndex:1], [array objectAtIndex:2]);
//但是打印的结果相同,说明para1和para2改变的是他们的副本,而保存在数组里面的是它们的原值
NSLog(@"%@, %@, %@", [array objectAtIndex:0], [array objectAtIndex:1], [array objectAtIndex:2]);
//指针地址没变,但是指针指向的对象的地址变了,说明para1盒para2改变的只是它的副本
NSLog(@"second %p, %p", &_para1, self.para1);
NSLog(@"second %p, %p", &_para2, self.para2);
//指针地址没变,指针指向的对象的地址也没变,说明是浅拷贝
NSLog(@"second %p, %p", &_para3, self.para3);
//把para3置空,检验数组是否还有para3
//执行次代码之后para3仍然存活,因为数组还在引用它。但我们不能假设它一定存活,释放它之后仍然打印它的地址是不好的习惯
self.para3 = nil;
NSLog(@"%p", self.para3);
//因为数组在para3上调用了retain,以期继续保留para3,所以它的引用计数为2,当self.para3释放之后,数组仍然拥有para3
NSLog(@"%p", [array objectAtIndex:2]);
}