OC 中 NSString 以及和 NSMutableStrin
Objective-C中的 NSString
是基础中的基础,现在总结一下 NSString
的知识点,以及和 NSMutableString
的区别。
NSString 的创建方法
最常见的方法就是使用 @ 符号+字符串
NSString *word = @"simple";
为什么要加 @ 符号呢?这里介绍一下 @ 符号,这是 OC 的语句符号,用来区分 C 语言,每当你看到 @ 符号,可以理解为:接下来执行的代码是 OC ,而不是 C 语言。
第二个注意的是 NSString
在 OC 中是一个类,是引用类型,初始化返回的是一个对象的指针,需要用 * 表示,这里word
代表指针,而不是 NSString 对象,如果没有 * 表示指针,则会编译不过:
NSString word = @"simple"; // Interface type cannot be statically allocated
这里的错误可以简单理解为,连接类型的数据(类),不能静态初始化(只能用指针初始化)。
其他常用的初始化方法包括 initWithFormat
:
NSString *words = [[NSString alloc] initWithFormat:@"There are %d cars on the road", 5]; // There are 5 cars on the road
上面等价于:
NSString *words = [NSString stringWithFormat:@"There are %d cars on the road", 5]; // There are 5 cars on the road
NSString 的不可修改性
NSString
是不能修改的,也就是说,给一个NSString
设定好初始值之后,值就不能更改了,比如下面的例子:
NSString *word = @"simple";
NSLog(@"%p", word); // 0x100004010
word = @"complex";
NSLog(@"%p", word); // 0x100004050
这里我们创建了一个NSString
word
,打印这个实例的地址,结果为0x100004010
,然后我们改变这个实例的值(改成 @complex
),发现这个实例的地址变成了 0x100004050
。
这说明当我们试图改变 NSString
的时候,实际上是把原来的字符串销毁,再创建一个新的字符串,同时更新指针。
NSString 常用的使用和比较方法
NSString
常用的操作方法有:
-
stringByReplacingOccurrencesofString
: 替换字符串内容
NSString *words = @"OC is harder than Swift";
NSLog(@"%@,%p", words, words); // OC is harder than Swift,0x100004018
NSString *newWords = [words stringByReplacingOccurrencesOfString:@"harder" withString:@"easier"];
NSLog(@"%@,%p", words, words); // OC is harder than Swift,0x100004018
NSLog(@"%@, %p", newWords, newWords); // OC is easier than Swift, 0x102b04180
我们可以看到,words
在使用 stringByReplacingOccurrencesofString
后地址和值并没有改变,这个方法返回了一个新的NSString
newWord
,地址和值都变了
下面的方法同样都不改变 NSString
本身:
-
stringByAppendingString
: 合并字符串
NSString *words = @"OC is harder than Swift";
NSLog(@"%@,%p", words, words); // OC is harder than Swift,0x100004018
NSString *newWords = [words stringByAppendingString:@". It is true"];
NSLog(@"%@,%p", words, words); // OC is harder than Swift,0x100004018
NSLog(@"%@, %p", newWords, newWords); // OC is harder than Swift. It is true, 0x100508310
-
substringFromIndex
: 截取字符串
NSString *words = @"OC is harder than Swift";
NSLog(@"%@,%p", words, words); // OC is harder than Swift,0x100004018
NSString *newWords = [words substringToIndex: 5];
NSLog(@"%@,%p", words, words); // OC is harder than Swift,0x100004018
NSLog(@"%@, %p", newWords, newWords); // OC is, 0xeea5967db477b839
-
containsString
:当 A 字符串包含 B 字符串时,返回 true。
NSString *words1 = @"OC is harder than Swift";
NSString *words2 = @"OC is harder";
NSLog(@"%@", [words1 containsString:words2] ? @"true" : @"false"); // true
-
isEqualToString
:当字符串完全相等时,返回 true。
NSString *words1 = @"OC is harder than Swift";
NSString *words2 = @"OC is harder";
NSLog(@"%@", [words1 isEqualTo:words2] ? @"true" : @"false"); // false
NSMutableString
NSMutableString
是可变的,我们不能用@ 符号+字符串(创建NSString
的方法)创建NSMutableString
:
NSMutableString *words = @"OC is harder than Swift"; // Incompatible pointer types initializing 'NSMutableString *' with an expression of type 'NSString *'
虽然上面可以编译通过(报warning),但这样做很危险,这相当于在一个可变的字符串中放了一个不可变的字符串,如果你改变这个字符串,就会报错:
[words setString:@"OC is easier than Swift"]; // Thread 1: "Attempt to mutate immutable object with setString:"
那么正确的创建 NSMutableString
的姿势是什么呢?常见的有一下两种:
NSMutableString *words1 = [@"OC is harder than Swift" mutableCopy];
NSMutableString *words2 = [[NSMutableString alloc] initWithFormat:@"OC is eaiser than Swift"];
上面我们知道了不能将NSSting
赋给NSMutableString
,否则在改变NSMutaleString
的时候会报错,如果将 NSMutableString
赋给NSSting
的时候会发生什么情况呢?看看下面的例子:
NSMutableString *words1 = [@"OC is harder than Swift" mutableCopy];
NSString *words2 = words1;
[words1 setString:@"OC is eaiser than Swift"];
NSLog(@"%@", words2); // OC is eaiser than Swift
可以看到,改变NSMutableString
会改变NSString
!因为words2
是指针,他指向了一个NSMutableString
,虽然他自身是一个定义为NSString
的指针。
解决方法是使用拷贝的方法copy
:
NSMutableString *words1 = [@"OC is harder than Swift" mutableCopy];
NSString *words2 = [words1 copy];
[words1 setString:@"OC is eaiser than Swift"];
NSLog(@"%@", words2); // OC is harder than Swift
这里拷贝了words1
的值,从而word2
不再指向word1
,而是指向一个新的NSString
。
另一个和NSString
不同的地方在于,NSMutableString
的多了很多对自己的操作的新方法,比如replaceOccurrencesOfString
NSMutableString *words = [@"OC is harder than Swift" mutableCopy];
NSLog(@"%@,%p", words, words); // OC is harder than Swift,0x10072e2d0
[words replaceOccurrencesOfString:@"harder" withString:@"easier" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [words length])];
NSLog(@"%@,%p", words, words); // OC is easier than Swift,0x10072e2d0
这里使用replaceOccurrencesOfString
方法后,words
的地址没变,指向的字符串的值变了。
再比如appendString
:
NSMutableString *words = [@"OC is harder than Swift" mutableCopy];
NSLog(@"%@,%p", words, words); // OC is harder than Swift,0x10072c6f0
[words appendString:@". It is true!"];
NSLog(@"%@,%p", words, words); // OC is harder than Swift. It is true!,0x10072c6f0
这里也是使用appendString
方法后,words
的地址没变,指向的字符串的值变了。