Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7098125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:01:00+00:00 2026-05-28T11:01:00+00:00

I just put up a wrong answer (deleted) The code was in response to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:01:01+00:00Added an answer on May 28, 2026 at 11:01 am

    The setter you wrote is incorrect for the reason that it doesn’t make a mutable copy of the string which is passed in.

    But I think your real question is why you don’t see an exception here. The reason is: you haven’t actually used the bad setter in the code you posted here.

    Here is the code you posted:

    [self.firstName setString:@"some words"];

    This is basically the same as this:

    NSMutableString *tmp = [self firstName]; // this is using your getter
    [tmp setString:@"some words"]; // this is calling a method on NSMutableString
    

    So first you use your getter, which correctly returns an NSMutableString*. Then you call a method that is already defined in Foundation called -[NSMutableString setString:]. This works fine because you are in fact sending it to an NSMutableString. So far you have avoided any issue.

    Where you would have gotten an exception is something like this:

    // assume myObj is an instance of whatever class has this 'firstName' property
    
    [myObj setFirstName:@"Some static and immutable string"]; 
    // OOPS! Now we've used the broken setter 
    // and firstName is now NOT a mutable string!
    
    [myObj.firstName setString:@"Some other string"]; 
    // ERROR. Now we've tried to send setString to an immutable string object. 
    

    I hope that helps.

    BTW, it sounds like you are using ARC. In that case, a correct setter can be as simple as this:

    -(void)setFirstName:(NSMutableString *)someString{
    
        _firstName =  [someString mutableCopy] ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This question is solved, but I can't accept my own answer just yet.
First of all, do not be overwhelmed by the long code, I just put
Okay, this is (probably) a very simple question, but I am afraid I know
Just learning rails, developing first app and having trouble finding a straight answer to
Just above the answer box an error would appear on wrong attempt which says
This surely is a noob question, but I can't find an answer in Doxygen
Update: I can't delete this question, because the answer has been upvoted, yet it
I've just put my new server up on an IP address with a domain
I'd like to just put in a comment in the block of my if-statement,
In C# i just put the method in the parentheses that i want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.