gpt4 book ai didi

objective-c - objective-c : convert a NSMutableString in NSString

转载 作者:IT老高 更新时间:2023-10-28 11:28:42 34 4
gpt4 key购买 nike

我有一个 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 - objective-c : convert a NSMutableString in NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5583510/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com