Reputation: 3048
I have an NSMutableString
called makeString
. I want to create it at the beginning of my program without having to set its text. I then want to be able to set its text. I am currently using the following to create it.
NSMutableString *make2String = [[NSMutableString alloc] initWithString:@""];
I am then using the following to set its text value.
make2String = [NSString stringWithFormat:@"Gold.png"];
Is this ok to do or is there a better way to set an NSMutableString
's text?
Upvotes: 2
Views: 2764
Reputation: 774
make2String = [NSMutableString stringWithFormat:@"Gold.png"];
FYI: This is how I allocate NSMutableStrings without setting text
NSMutableString *string = [[NSMutableString alloc] init];
Upvotes: 0
Reputation: 94844
That is not ok, you are replacing your mutable string with an ordinary immutable string (and leaking the original mutable string in the process). You could do [NSMutableString stringWithFormat:@"Gold.png"]
after releasing the old string if you wanted to go that route. Or you could use NSMutableString's setString:
method to set the content.
But if you're not actually mutating the string and just assigning different strings, you don't need NSMutableString at all. Just do make2String = @"Gold.png";
and be done with it.
Upvotes: 4
Reputation: 31171
NSMutableString * aString = [NSMutableString alloc];
aString = [aString init];
[aString setString:@"yourText"];
[aString setString:@"yourNewText"];
[aString setString:@"yourNewNewText"];
//...
[aString release];
Upvotes: 1