Appending Strings to NSMutableString

typeoneerror picture typeoneerror · Apr 1, 2010 · Viewed 8.2k times · Source

Been looking at this for a bit now and not understanding why this simple bit of code is throwing an error. Shortened for brevity:

NSMutableString *output;

...

@property (nonatomic, retain) NSMutableString *output;

...

@synthesize output;

...

// logs "output start" as expected
output = [NSMutableString stringWithCapacity:0];
[output appendString:@"output start"];
NSLog(@"%@", output);

...

// error happens here
// this is later on in a different method
[output appendString:@"doing roll for player"];

Can anyone spot my mistake?

Answer

Nick Moore picture Nick Moore · Apr 1, 2010

Change the line

output = [NSMutableString stringWithString:@"output start"]

to

[self setOutput:[NSMutableString stringWithString:@"output start"]]

(or self.output = ... if you prefer that notation).

Although you have declared a property, you are not using the setter, so you are not retaining the string.