I just put up a wrong answer (deleted)
The code was in response to this question. The OP wanted to know why they got a Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with setString:
(My answer was a calamity of me not paying attention to what I was doing, being tired and still learning. The usual excuses. :-) )
When they tried to set the mutable string with:
[firstName setString:@""];
The property is a NSMutableString
@property (copy,nonatomic) NSMutableString* firstName
I posted a bit of code that was working for me. But mistook the getter for the setter. (I am still new, and tired :-) )
Now what is confusing is once it was pointed out that I was wrong. I re-looked at my code and realised what I had done.
But in my project I only had the setter synthesised and not declared.
@synthesize firstName =_firstName;
And I had declared the getter like so:
-(NSMutableString *)firstName{
if (!_firstName) _firstName = [[NSMutableString alloc]init];
return _firstName;
}
But all was working with no issue without me declaring a setter. Which your supposed to do for a property for a mutable object and (copy)
If put a setter in :
-(void)setFirstName:(NSMutableString *)mutableString{
_firstName = mutableString ;
}
It still works all ok. I use the call:
[self.firstName setString:@"some words"];
I did get the Exception once when I think I first removed the getter and leaving the setter. But I cannot repeat the error!
I hope this is clear..
Does any one know what is going on. And am I doing the setter and getter correctly in this case.
Thanks