NSString *str3 = @"1234567890";
initWithString、stringWithString编译器会警告:对文本使用“initWithString:”是多余的
NSString *str4 = [[NSString alloc] initWithString:@"1234567890"]; //-1 常量区 __NSCFConstantString
NSString *str5 = [[NSString alloc] initWithString:@"12345678902"]; //-1
NSString *str6 = [NSString stringWithString:@"1234567890"]; //-1
NSString *str7 = [NSString stringWithString:@"12345678902"]; //-1
NSString *str8 = [NSString stringWithString:@"ew23re34rf4w3"]; //-1
NSString *str9 = [NSString stringWithFormat:@"%@",@"1234567890"]; // 1 堆区 __NSCFString
NSString *str10 = [[NSString alloc] initWithFormat:@"%@",@"1234567890"]; // 1 堆区
NSString *str11 = [[NSString alloc] initWithFormat:@"%@",@"1234567890"]; // 1 堆区
NSString *str12 = [[NSString alloc] init]; // -1
NSString *str13 = @"3"; // -1
NSString *str14 = [NSString stringWithFormat:@"%@",@"127890"]; // -1 Tagged Pointer : NSTaggedPointerString
NSString *str15 = [[NSString alloc] initWithFormat:@"%@",@"126790"]; // -1 Tagged Pointer
NSString *str16 = [[NSString alloc] initWithString:@"126790"]; //-1
NSString *str17 = [NSString stringWithString:@"126790"]; //-1
1. 用stringWithFormat、initWithFormat创建的string,长度<10,NSTaggedPointerString类型,引用计数为 -1;长度>=10,NSCFString,存在堆区,引用计数为 1;
2.@""、initWithString、stringWithString创建的,无论长短,都存在常量区,NSCFConstantString,引用计数为 -1
3.程序员只能控制堆区变量对象的引用计数,其他区域的不能更改。
Tagged Pointer专门用来存储小的对象,例如NSNumber和NSDate
Tagged Pointer指针的值不再是地址了,而是真正的值。所以,实际上它不再是一个对象了,它只是一个披着对象皮的普通变量而已。所以,它的内存并不存储在堆中,也不需要malloc和free。
NSLog(@"3:%ld %p %s",[str3 retainCount],str3,object_getClassName(str3));
NSLog(@"4:%ld %p %s",[str4 retainCount],str4,object_getClassName(str4));
NSLog(@"5:%ld %p %s",[str5 retainCount],str5,object_getClassName(str5));
NSLog(@"6:%ld %p %s",[str6 retainCount],str6,object_getClassName(str6));
NSLog(@"7:%ld %p %s",[str7 retainCount],str7,object_getClassName(str7));
NSLog(@"8:%ld %p %s",[str8 retainCount],str8,object_getClassName(str8));
NSLog(@"9:%ld %p %s",[str9 retainCount],str9,object_getClassName(str9));
NSLog(@"10:%ld %p %s",[str10 retainCount],str10,object_getClassName(str10));
NSLog(@"11:%ld %p %s",[str11 retainCount],str11,object_getClassName(str11));
NSLog(@"12:%ld %p %s",[str12 retainCount],str12,object_getClassName(str12));
NSLog(@"13:%ld %p %s",[str13 retainCount],str13,object_getClassName(str13));
NSLog(@"14:%ld %p %s",[str14 retainCount],str14,object_getClassName(str14));
NSLog(@"15:%ld %p %s",[str15 retainCount],str15,object_getClassName(str15));
NSLog(@"16:%ld %p %s",[str16 retainCount],str16,object_getClassName(str16));
NSLog(@"17:%ld %p %s",[str17 retainCount],str17,object_getClassName(str17));
[str3 retain];
[str13 retain];
NSLog(@"3:%ld %p %s",[str3 retainCount],str3,object_getClassName(str3));
NSLog(@"13:%ld %p %s",[str13 retainCount],str13,object_getClassName(str13));
2019-12-22 15:03:56.020087+0800 zhengze[21303:4855101] 3:-1 0x10c636428 __NSCFConstantString
2019-12-22 15:03:56.020168+0800 zhengze[21303:4855101] 4:-1 0x10c636428 __NSCFConstantString
2019-12-22 15:03:56.020229+0800 zhengze[21303:4855101] 5:-1 0x10c636448 __NSCFConstantString
2019-12-22 15:03:56.020282+0800 zhengze[21303:4855101] 6:-1 0x10c636428 __NSCFConstantString
2019-12-22 15:03:56.020341+0800 zhengze[21303:4855101] 7:-1 0x10c636448 __NSCFConstantString
2019-12-22 15:03:56.020401+0800 zhengze[21303:4855101] 8:-1 0x10c636468 __NSCFConstantString
2019-12-22 15:03:56.020471+0800 zhengze[21303:4855101] 9:1 0x600003c8ea60 __NSCFString
2019-12-22 15:03:56.020538+0800 zhengze[21303:4855101] 10:1 0x600003c8d3e0 __NSCFString
2019-12-22 15:03:56.020594+0800 zhengze[21303:4855101] 11:1 0x600003c8d340 __NSCFString
2019-12-22 15:03:56.020671+0800 zhengze[21303:4855101] 12:-1 0x10cc38178 __NSCFConstantString
2019-12-22 15:03:56.020736+0800 zhengze[21303:4855101] 13:-1 0x10c636488 __NSCFConstantString
2019-12-22 15:03:56.041055+0800 zhengze[21303:4855101] 14:-1 0xeb31abb5e1b45035 NSTaggedPointerString
2019-12-22 15:03:56.041154+0800 zhengze[21303:4855101] 15:-1 0xeb31abb511a45035 NSTaggedPointerString
2019-12-22 15:03:56.041279+0800 zhengze[21303:4855101] 16:-1 0x10c6364c8 __NSCFConstantString
2019-12-22 15:03:56.041416+0800 zhengze[21303:4855101] 17:-1 0x10c6364c8 __NSCFConstantString
2019-12-22 15:03:56.041480+0800 zhengze[21303:4855101] 3:-1 0x10c636428 __NSCFConstantString
2019-12-22 15:03:56.041535+0800 zhengze[21303:4855101] 13:-1 0x10c636488 __NSCFConstantString
iOS NSString引用计数、存储区域、数据类型
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...