我有一个NSMutableString,如何将其转换为NSString?

推荐答案

通过以下途径之一:

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;

使它看起来是不变的,即使它实际上是可变的

Objective-c相关问答推荐

将 NSInteger 转换为 NSIndexpath

我应该为 memcpy 和 realloc 包含什么标题?

iOS中的非延迟图像加载

iphone 的 UIPopoverController 不工作?

dispatch_get_global_queue 和 dispatch_queue_create 有什么区别?

自动布局和按下时隐藏底栏

NSPredicate 与整数比较

如何测试推送通知在我的应用程序中是否有效

滚动后检测 UITableView 中的当前顶部单元格

如何删除应用程序指定目录中的所有文件?

为什么是 [object doSomething] 而不是 [*object doSomething]?

带有块的 Objective-C 延迟动作

如何推送两个视图控制器但只为第二个设置动画过渡?

在 32 位和 64 位架构上格式化 NS(U)Integer 时类型转换的替代方法?

HTML 内容适合 UIWebview 而无需缩小

Objective-C 中的 NSString indexOf

cellForRowAtIndexPath 是如何工作的?

协议和代表之间的区别?

为什么 detailTextLabel 不可见?

Objective-C in,out,inout,byref,byval, .. 等等.这些是什么?