#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSString *str1 = @"xiaoming";
NSString *str2 = [NSString stringWithFormat:@"xiaoming"];
NSString *str3 = [NSString stringWithFormat:@"xiaoming"];
NSString *str4 = @"xiaoming";
NSLog(@"\nstr1: %p\nstr2: %p \nstr3: %p \nstr4: %p", str1, str2, str3, str4);
}
return 0;
}
2019-06-26 09:40:52.294925+0800 test[804:77187]
str1: 0x100001048
str2: 0xeae43525019535b5
str3: 0xeae43525019535b5
str4: 0x100001048
Program ended with exit code: 0
这段结果 :通过类方法stringWithFormat自定义创建的字符串会放在堆里面,即使字符串内容相同,也会再次开辟新空间,然后指针就是在栈里面,保存对象的地址 有所违背, 查找原因中
网友评论