I have two NSMutableString objects defined in my viewController’s (a subclass of UITableViewController) .h file:
NSMutableString *firstName;
NSMutableString *lastName;
They are properties:
@property (nonatomic, retain) NSMutableString *firstName;
@property (nonatomic, retain) NSMutableString *lastName;
I synthesis them in the .m file.
In my viewDidLoad method – I set them to be blank strings:
firstName = [NSMutableString stringWithString:@""];
lastName = [NSMutableString stringWithString:@""];
firstName and lastName can be altered by the user. In my cellForRowAtIndexPath method, I’m trying to display the contents of these strings:
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
but this is causing the app to crash as soon as the view controller is displayed. Using the debugger, it seems that both firstName and lastName are “out of scope” or that they don’t exist. I’m new to Xcode but the debugger seems to halt at objc_msgSend.
What am I doing wrong?
The problem is that you need to do:
That will call the auto-generated setter methods, which will retain the values. By just assigning directly, you are bypassing the setters, and as soon as the current autorelease pool is drained the two variables will have dangling pointers.