同时拥有NSMutableString和NSString的目的是什么? [英] What is the purpose of having both NSMutableString and NSString?
问题描述
为什么目标C同时提供NSString类和NSMutableString子类,而不是仅提供NSMutableString? NSString不等于"const NSMutableString"吗?
Why does Objective C provide both class NSString and subclass NSMutableString rather than just provide NSMutableString? Isn't a NSString equivalent to "const NSMutableString"?
在C ++中,只有一个字符串类std :: string,如果要使用常量,则可以声明const std:string.
In C++, you have only one string class, std::string, and if you want a constant you declare a const std:string.
我很想知道为什么我不应该只在各处使用NSMutableString,而从不理会NSString吗?必须有一个原因,否则语言设计者将不会同时提供两者.也许占用更少的存储空间?
I'm interested in knowing why I shouldn't just use NSMutableString everywhere and never bother with NSString? There must be a reason, or the language designers wouldn't provide both. maybe it takes up less storage or something?
推荐答案
两个类的原因与您有时使用std :: string和有时使用const std :: string的原因相同.但是,与C ++不同,Objective-C没有const方法,因此它们将const-和非const方法分成两个不同的类.在许多核心类中也可以看到这一点,例如NSArray(NSMutableArray),NSDictionary(NSMutableDictionary)等.
The reason for both classes is the same reason that you sometimes use a std::string and sometimes use a const std::string. However, unlike C++, Objective-C doesn't have const methods, so they instead separate const- from non-const- methods into two different classes. This is also seen in many of the core classes, such as NSArray (NSMutableArray), NSDictionary (NSMutableDictionary), etc.
这篇关于同时拥有NSMutableString和NSString的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!