Cra*_*Dev 62 objective-c nsstring nsmutablestring
我有一个NSMutableString,如何将其转换为NSString?
Reg*_*ent 95
通过:
NSString *immutableString = [NSString stringWithString:yourMutableString];
Run Code Online (Sandbox Code Playgroud)
或通过:
NSString *immutableString = [[yourMutableString copy] autorelease];
//Note that calling [foo copy] on a mutable object of which there exists an immutable variant
//such as NSMutableString, NSMutableArray, NSMutableDictionary from the Foundation framework
//is expected to return an immutable copy. For a mutable copy call [foo mutableCopy] instead.
Run Code Online (Sandbox Code Playgroud)
作为NSString的子类,但是您可以将其强制转换为NSString
NSString *immutableString = yourMutableString;
Run Code Online (Sandbox Code Playgroud)
使它看起来不可变,即使它实际上保持可变.
尽管声明返回不可变的实例,但许多方法实际上返回了可变实例.