如何从NSMutableString取出NSString? [英] How to take an NSString out of an NSMutableString?

查看:92
本文介绍了如何从NSMutableString取出NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点混乱。 NSMutableString继承自NSString,但是这也意味着我可以在任何地方安全地传递一个NSMutableString,而实际上是一个NSString。我可以分配NSString到NSMutableString吗?

That's somewhat confusing. NSMutableString inherits from NSString, but does that also mean that I can pass an NSMutableString anywhere safely where actually an NSString is wanted? And can I assign an NSString to an NSMutableString? How would I get an NSString out of an NSMutableString to avoid problems, if any?

推荐答案


  1. 如果有NSString从NSMutableString中获取NSString, p>是的,您可以为 NSString 传递 NSMutableString 。但是,请注意,如果传递给它的对象存储对此对象的引用,它将看到您对可变字符串对象所做的所有更改。
  1. Yes, you can pass an NSMutableString for an NSString. However, be aware that if the object you pass it to stores a reference to this object, it will "see" all the changes you make to the mutable string object. This is not always desirable.

不,不可能。如果你有一个 NSMutableString 指针指向 NSString 并调用,例如, appendString: / code>,对象不知道如何处理调用。

No, not possible. If you had a NSMutableString pointer pointing to an NSString and called, say, appendString: on it, the object wouldn't know how to process the call.

(前两个是OO包含多态性基础)

(the first two are OO inclusion polymorphism basics)

如果要从可变变量字符串中获取不可变字符串,请使用 [mutableString copy] p>

If you want to get a non-mutable string from a mutable one, use [mutableString copy].

这篇关于如何从NSMutableString取出NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆