Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of having both NSMutableString and NSString?

Why does Objective C provide both class NSString and subclass NSMutableString rather than just provide NSMutableString? Isn't a NSString equivalent to "const NSMutableString"?

In C++, you have only one string class, std::string, and if you want a constant you declare a const std:string.

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?

like image 738
progrmr Avatar asked Nov 17 '09 15:11

progrmr


People also ask

What is difference between string and NSString in Swift?

The Swift string is one character long, as expected. The NSString says it has a length of seven — this matches with the length of the Swift string's utf16 view, since NSStrings are backed by UTF-16: 09:02 The Swift string's unicodeScalars view returns a count of four.

What does NSString mean?

A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.

What is NSMutableString?

NSMutableString is the abstract base class for a cluster of classes representing strings whose contents can be changed. It inherits from NSString. It extends the interface it inherits from NSString by adding methods to change the string contents.

What is NSString in objective C?

(NSString *) is simply the type of the argument - a string object, which is the NSString class in Cocoa. In Objective-C you're always dealing with object references (pointers), so the "*" indicates that the argument is a reference to an NSString object.


2 Answers

It is very possible, and even likely, that there are optimizations in place that are only allowed when strings are immutable.

In fact running

NSString *A = @"Bob";
NSString *B = @"Bob";

in the debugger immediately shows that they are both pointers to the same string. In fact

NSString *C = [NSString stringWithString:@"Bob"];
NSString *D = [A copy];

both point to the same memory address as well. Meanwhile

NSString *E = [NSMutableString stringWithString:@"Bob"];

points to a different string.

So yes, using NSStrings are more efficient in some cases. And in general cocoa lends itself to returning a new copy of a string rather than an edited one. However, I can't really argue that you shouldn't use a mutable string everywhere, but it does seem to go against the general guidelines for the framework.

In my own work I tend to only use mutable variants where I need to edit things directly. It's just a little backwards from the C/C++ style of everything mutable unless you need a const, everything is const unless you need mutability.

like image 186
Bryan McLemore Avatar answered Oct 18 '22 01:10

Bryan McLemore


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.

like image 38
Daniel Yankowsky Avatar answered Oct 18 '22 00:10

Daniel Yankowsky