copy与内存管理

1.copy与内存管理

  • 浅拷贝

    • 原对象引用计数器+1
    • 必须对原对象进行释放
char *cstr = "this is a c string";
NSString *str1 = [[NSString alloc] initWithUTF8String:cstr];
NSLog(@"str = %lu", [str1 retainCount]);
NSString *str2 = [str1 copy];
NSLog(@"str = %lu", [str1 retainCount]);
[str2 release];必须做一次release
  • 深拷贝

    必须释放新对象

char *cstr = "this is a c string";
NSString *str1 = [[NSString alloc] initWithUTF8String:cstr];
NSLog(@"str = %lu", [str1 retainCount]);
NSMutableString *str2 = [str1 mutableCopy];
NSLog(@"str = %lu", [str1 retainCount]);
[str2 release]; // 必须做一次release
[str1 release];
 

 

posted @ 2017-03-30 22:06  iFat  阅读(126)  评论(0编辑  收藏  举报