Mutable vs. Immutable Strings

Discussion in 'Mac Programming' started by MadDoc, Oct 31, 2007.

  1. macrumors 6502

    MadDoc

    #1
    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,
     
  2. Administrator

    HexMonkey

    Staff Member

    #2
    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.
     
  3. macrumors 601

    HiRez

    #3
    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:).
     
  4. macrumors 6502

    MadDoc

    #4
    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,
     

Share This Page