The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 5 of 5

Thread: NSMutableString vs NSString.

  1. #1

    NSMutableString vs NSString.

    So, the documentation says:

    Quote Originally Posted by AppleDocs
    The NSString class declares the programmatic interface for an object that manages immutable strings. (An immutable string is a text string that is defined when it is created and subsequently cannot be changed.
    Why am I able to change the value of the NSString object in this code? I feel like I'm missing something.

    Code:
    #import <Foundation/NSString.h>
    #import <Foundation/NSObject.h>
    #import <Foundation/NSAutoreleasePool.h>
    
    int main(int argc, char *argv[])
    {
    	NSAutoreleasePool *pool = [NSAutoreleasePool new];
    	
    	NSString *myStr = @"I can haz programburger";
    	
    	myStr = @"banana";
    	
    	printf("\n %s \n", [myStr UTF8String]); //Returns "banana"
    	
    	[pool release];
    	
    	return 0;
    }

  2. #2
    You're not actually modifying any string, you're just changing the myStr to point to a different one. The "I can haz programburger" still exists unmodified in memory.

    The difference between NSString and NSMutableString then is that NSMutableString objects provide methods to modify the underlying array of characters they represent, while NSString does not. For example, NSMutableString exposes methods such as appendString, deleteCharactersInRange, insertString, replaceOccurencesWithString, etc. All these methods operate on the string as it exists in memory.

    NSString on the other hand only is a create-once-then-read-only string if you will; you'll find that all of its "manipulation" methods (substring, uppercaseString, etc) return other NSString objects and never actually modify the existing string in memory.

    If you know Java, you can see NSString as Java's String and NSMutableString as Java's StringBuilder.
    Wait, what?

  3. #3
    Okay, thank you, that makes sense.

  4. #4
    Quote Originally Posted by Voetsjoeba View Post
    You're not actually modifying any string, you're just changing the myStr to point to a different one. The "I can haz programburger" still exists unmodified in memory.
    One related question I have, if we know we won't need the string "I can haz programburger" anymore, what should we do so that it doesn't remain in memory? I know it won't get in the way, but just as a matter of good memory management practice?
    Code:
    NSString *myStr = @"I can haz programburger";
    myStr = @"banana";

  5. #5
    Quote Originally Posted by anroy View Post
    One related question I have, if we know we won't need the string "I can haz programburger" anymore, what should we do so that it doesn't remain in memory? I know it won't get in the way, but just as a matter of good memory management practice?
    Code:
    NSString *myStr = @"I can haz programburger";
    myStr = @"banana";
    As far as I know @"..." is a compile time constant that doesn't need to be released.

    Any other part of your program that contains @"I can haz programburger" should reference to the same object, thus releasing it could cause trouble (well, it has an retainCount of 0xFFFFFFFF anyway ).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012