PDA

View Full Version : Mutable vs. Immutable Strings




MadDoc
Oct 31, 2007, 05:23 AM
I am trying to learn Objective-C and Cocoa. I think I am making slow but steady progress :)

I was just wondering if anyone can explain in simple English the difference between NSMutable strings and NSStrings (i.e. immutable). I just don't understand the difference.

Sorry for such a simple question!

Thanks,

MadDoc,



HexMonkey
Oct 31, 2007, 05:40 AM
Mutable strings can be modified after they've been created, immutable strings cannot. In other words, you can do things like add text to an NSMutableString object, or remove part of it, but you can't do these things to an NSString object without creating a new object.

HiRez
Oct 31, 2007, 03:46 PM
And immutable strings are generally more efficient in several ways, lest you be tempted to always use mutable strings. Try to always use NSStrings where you can and go mutable only when you really need to build up strings dynamically (although even here you often don't need to do this thanks to methods like -stringWithFormat:).

MadDoc
Oct 31, 2007, 04:56 PM
Thanks guys.

Having come from REALbasic - I am finding ObjC/Cocoa to have a steep learning curve but I think the effort will be worth it.

On the subject, are there any new resources for IB 3 out there yet?

MadDoc,