- #import <Foundation/Foundation.h>
- #import "Person.h"
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- //@property的参数copy的用法
- NSMutableString *str = [NSMutableString string];
- [str appendString:@"秦不来"];
- NSLog(@"str.add = %p,str.ratainCount = %lu",str,str.retainCount);
- Person *p = [Person new];
- p.name = str;
- NSLog(@"str.add = %p,str.ratainCount = %lu",str,str.retainCount);
- NSLog(@"p.name.add = %p,p.name.ratainCount = %lu",p.name,p.name.retainCount);
- }
- return 0;
- }
复制代码
上面代码运行的结果:
2015-12-11 21:44:18.848 Copy和内存管理[742:20439] str.add = 0x100100760,str.ratainCount = 1
2015-12-11 21:44:18.849 Copy和内存管理[742:20439] str.add = 0x100100760,str.ratainCount = 1
2015-12-11 21:44:18.849 Copy和内存管理[742:20439] p.name.add = 0x100503430,p.name.ratainCount = 1
结果分析:copy过后,内存中重新分配了内存空间用来存储str 的副本,因此@property的参数copy实现的是深复制。 |
|