开发者

In Obj-C How to update an existing NSString variable with a new formatted string?

开发者 https://www.devze.com 2023-01-05 12:41 出处:网络
How do i update an existing NSString variable with a new formatted string? for example i have a variable like this:

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 开发者_StackOverflow中文版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?


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.


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:..]


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号