user1374408
user1374408

Reputation: 321

Usage of NSMutableString vs NSString?

I am confused between NSString and NSMutable string usage. Suppose I have instance variables one and two declared in class like this:

NSString *one;
NSMutableString *two;

let us suppose I have created setters and getters of them using properties.

lets say I have changed my "two" string like these:

     1. [two appendString:@"more string"];
     2. two = @"string"
     3. self.two = @"string"

Questions:

  1. would 1st line release previous string and allocate new object and assign value to it. if yes, then does that mean creating getters and setters are unnecessary in this case ? OR its unnecessary to create properties in NSMutablestring

  2. In this case would the previous allocated string object released ?

  3. Its for sure that first object would be released as we are calling setters here. is this code necessary or we can just use the code line 2 to assign string.

Now about NSString: As can modify the string like this also :

    one = [one stringByAppendingString:@" more string"];
    self.one = [one stringByAppendingString:@" more string"];

Which is better using NSMutablestring or NSString ?

Sorry for long post, but I needed to understand these concepts.

Upvotes: 4

Views: 4270

Answers (4)

Sam
Sam

Reputation: 41

NSString is a not mutable string object, which means, after initialization, you cannot change it, NSMutableString is mutable meaning you can append another string to it or other modifications.

when you do [two appendString:@"more string"], the pointer still pointed to the same location in memory and you don't have to worry about allocation or deallocation.

When you do two = @"string" , it means you make your string pointer pointed to a specific location in memory which contains static string with value "string" inside.

When you do self.two = @"string", you've already have a property named two declared. By using property in xcode, you don't have to worry about the memory since you've already specified in your property declaration. It is a nice tool xcode provide to you, and you should definitely use it whenever you can.

Upvotes: 3

AndersK
AndersK

Reputation: 36092

NSString objects are immutable this means that when you "modify" a NSString you are in fact creating a new string and not modifying the old which is not very effective. With NSMutableString the string is kept in a buffer than can be modified.

So the simple rule is that if you need to modify the string in any way use a NSMutableString and if not NSString. You can also cast a NSMutableString to a NSString but not the other way around (then you need to make a copy)

Upvotes: 2

Mundi
Mundi

Reputation: 80271

NSMutableString will save some memory as NSString objects are always constants. So reassigning a new value to an NSString variable will allocate new memory for the new string.

However, please note a few errors in your code. You will have to initialize the objects before sending messages to them:

// this won't work
NSString *one; 
[one stringByAppendingString:@"more string"];

// nor this
NSMutableString *two;
[two appendString:@"more string"];

// first do this: 
NSString *one = @"an initial string constant"; // or
NSString *one = [NSString string]; 

NSMutableString *two = [NSMutableString string]; 

Upvotes: 2

James Webster
James Webster

Reputation: 32066

For the first part of your question:

  1. Almost definitely not. I expect the memory will be allocated dynamically, rather than released and reallocated.

  2. If the previous answer is no, this is also, no.

  3. The second and third options don't even work with the warning Incompatible pointer types assigning NSMutableString to NSString

I expect NSMutableString will be slightly more efficient in terms of memory as you are indicating early on that the program may need memory dynamically. NSString is likely to be allocated a single, suitably sized block of memory.

Your views of NSMutableString seem to be what the stringBy.. methods of NSString will do:

With NSString and its stringBy... methods, you are creating new objects, releasing the old one (if need be) and making the new object autorelease. (Take care if you are changing from non-autorelease to autorelease, you may have a release in your dealloc that isn't needed anymore)

Upvotes: 3

Related Questions