A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 余丽丽 高级黑马   /  2015-7-25 09:14  /  528 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

NSString *str1 = @"ABC";
NSString *str2 = [NSString stringWithFormat: @"aaa"];
NSString *str3 = [NSString stringWithString: @"ABC"];
NSString *str4 = [[NSString alloc]initWithFormat: @"aaa"];
NSString *str5 = [[NSString alloc]initWithString: @"ABC"];

NSLog(@"str1 = %@,%p,%lu",str1,str1,str1.retainCount);
NSLog(@"str2 = %@,%p,%lu",str2,str2,str2.retainCount);
NSLog(@"str3 = %@,%p,%lu",str3,str3,str3.retainCount);
NSLog(@"str4 = %@,%p,%lu",str4,str4,str4.retainCount);
NSLog(@"str5 = %@,%p,%lu",str5,str5,str5.retainCount);

运行结果:

2015-07-18 10:50:42.559 NSString内存管理[1260:64557] str1 = ABC,0x100001030,18446744073709551615
2015-07-18 10:50:42.560 NSString内存管理[1260:64557] str2 = aaa,0x61616135,18446744073709551615
2015-07-18 10:50:42.560 NSString内存管理[1260:64557] str3 = ABC,0x100001030,18446744073709551615
2015-07-18 10:50:42.560 NSString内存管理[1260:64557] str4 = aaa,0x61616135,18446744073709551615
2015-07-18 10:50:42.560 NSString内存管理[1260:64557] str5 = ABC,0x100001030,18446744073709551615


结论:
@"ABC"  stringWithString   initWithString  都在常量区
@"aaa"  stringWithFormat   initWithFormat  都在堆区

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马