*/
#import <Foundation/Foundation.h>
int main() {
//打印出的两个str地址不一样
//str重新赋值,为指针由指向jack的空间改指向新创建rose的空间,而jake的空间地址和存储内容都不可变
NSString *str = @"jack";
NSLog(@"str addr = %p", str);
str = @"rose";
NSLog(@"str addr = %p", str);
//打印出的str2的地址一样,但内容变化
//str2追加一个字符串,str2指向的空间没有变化,追加的内容直接加到该空间里面,拼接到原内容后面
NSMutableString *str2 = [NSMutableString stringWithFormat:@"jack"];
NSLog(@"str2 addr = %p",str2);
NSLog(@"str2 = %@",str2);
[str2 appendString:@"ross"];
NSLog(@"str2 addr = %p",str2);
NSLog(@"str2 = %@",str2);
//10个itcast组成字符串
//随着追加字符串空间逐渐加大,足以容纳新的字符串,所以str3空间被改变
NSMutableString *str3 = [NSMutableString string];
NSLog(@"str3 = %@",str3);
for (int i = 0; i<10; i++) {
if (!i) {
[str3 appendString:@"--itcast"];
}
[str3 appendString:@" itcast"];
}
NSLog(@"str3 = %@",str3);
return 0;
}
|
|