views:

47

answers:

4

How do i update an existing NSString variable with a new formatted string?

for example i have a variable like this:

String1 = [NSString new];

I want this string object to be updated from time to time with new formatted contents using the standard printf format.

I can initialise a new NSString using the initWithFormat: message but this is unavailable to already instantiated objects.

Any ideas? I'm thinking i can destroy the NSString each time and re-initialise a new one but is this the correct solution each time i need to update it?

+3  A: 

Strings in Cocoa are immutable objects.

This means that you won't change the same string but you will just free the old one and allocate a new NString with your updated content.

Of course this is not a problem since you will have a NSString* reference that will point to the last updated string.

NSString myString = [NSString stringWithFormat:...];

// do whatever

myString = [NSString stringWithFormat:..]
Jack
Would you need to add `autorelease` to these strings as they are using a class method?
Gary Willoughby
Gary, in this case the answer is no because stringWithFormat returns an autoreleased NSString.
mharper
ok, thanks.....
Gary Willoughby
+3  A: 

Two options:

  1. Create a new string every time with [[NSString alloc] initWithFormat:@"whatever"] and assign it to the variable. (Make sure you follow the memory management rules, which includes making sure the string's previous value is released. Of course, you'll need to follow those rules no matter how you tackle this problem.)

  2. Create an NSMutableString and update the string with the mutating methods (appendFormat:, setString:, deleteCharactersInRange:, etc.). In this case, you're not just updating the variable, but the string itself.

Personally, I would use method 1, creating a new NSString every time. That way I don't have to fiddle with mutation and can just create a string with the precise value I want.

Chuck
A: 

There is an NSMutableString which can be modified after the initialization. You can add a formatted string using appendStringWithFormat:. But if you want to replace the entire string, just create another. It doesn't cost much time and resources.

It depends on the taste, but I am against re-using a variable as Jack suggested. It often just confuses me, I prefer to create a new variable name whenever I create a new string, as in

 NSString* myString = [NSString stringWithFormat:...];

 // do whatever

 NSString*myString2 = [NSString stringWithFormat:...];

Another thing is that new is not widely-used in Objective-C world. If you use that because it sounds familiar from your C++ background etc., consider using [[Class alloc] initWith...] instead.

Yuji
A: 

The simplest thing to do is just always use "factory" methods that give you an autoreleased immutable object. In fact, you basically do that whenever you create an NSString that holds some static text.

For example:

NSString *myString = @"myString";
myString = [NSString stringWithFormat:@"myStringWithFormat"]; // Update it to something else
myString = [NSString stringWithFormat:@"The current date is: %@", [NSDate now]]; // Update it again

All three of the above examples are immutable strings that have a collective ref count of 0. You do not need to worry about releasing or allocating them.

mdizzle