通过以下途径之一:
NSString *immutableString = [NSString stringWithString:yourMutableString];
或通过:
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.
作为NSString的子类,您可以将其强制转换为NSString
NSString *immutableString = yourMutableString;
使它看起来是不变的,即使它实际上是可变的