NSString的retainCount

提示 :  为了方便查看引用计数, ARC环境修改为MRC, 步骤如下

选择项目 Target -> Build Sttings -> All -> 搜索‘automatic’ -> 把 Objective-C Automatic Reference Counting 设置为 NO,如下图:

NSString的retainCount_第1张图片
arc-->mrc

类型                                                                                              初始化后的retainCount

__NSCFConstantString (retain/release不起作用,系统进行维护,程序中内容相同的常量字符串只有一个)                                                                             -1

__NSCFString (retain/release有作用)                                                    1

__NSTaggedPointerString (retain/release不起作用)                         -1

__NSCFConstantString类型如下:

NSString *str = @"test"; //__NSCFConstantString

NSLog(@"%d",[str retainCount]);  // -1

NSString *str = [NSString stringWithString:@"test"]; //__NSCFConstantString

NSLog(@"%d",[str retainCount]);  // -1

__NSTaggedPointerString类型如下:

NSString *str = [NSString stringWithFormat:@"%s","test"];//__NSTaggedPointerString

NSLog(@"%d",[str retainCount]);   // -1

 NSString *a = @"a"; NSString *b = [[a mutableCopy] copy];//__NSTaggedPointerString

NSLog(@"%d",[b retainCount]); // -1

__NSCFString类型如下 :

通过stringWithFormat构造

 NSString *str = [NSString stringWithFormat:@"%s,%@","test",@"test"];//__NSCFString

  NSLog(@"%d",[str retainCount]); // 1

stringWithString + stringWithFormat构造

NSString *str = [NSString stringWithString:[NSString stringWithFormat:@"test"]];//__NSCFString

NSLog(@"%d",[str retainCount]); // 1

NSString *str = [NSString stringWithString:[NSString stringWithFormat:@"%s","test"]];//__NSCFString

NSLog(@"%d",[str retainCount]); //1

NSString *str = [NSString stringWithString:[NSString stringWithFormat:@"%s,%d","test",10]];//__NSCFString

NSLog(@"%d",[str retainCount]); // 1

stringWithString+__NSTaggedPointerString(类型) 构造

    NSString *str = [NSString stringWithFormat:@"%s", "str2"];//__NSTaggedPointerString

    NSString *str1 = [NSString stringWithString:[NSString stringWithFormat:@"%s", "str2"]];

    NSLog(@"%d",[str1 retainCount]); //1

stringWithString+__NSCFString(类型) 构造

    NSString *str = [NSString stringWithFormat:@"%s,%d", "test",10];//__NSCFString

    NSString *str1 = [NSString stringWithString:str];

    NSLog(@"%d",[str1 retainCount]); //2

NSMutableString(对象)构造

  NSMutableString* str = [NSMutableString stringWithString:@"test"];//__NSCFString

  NSLog(@"%d",[str retainCount]); //1

你可能感兴趣的:(NSString的retainCount)