0

I have the following statement in my program:

[operateAns replaceCharactersInRange:NSMakeRange(start, end-start) withString:[answer stringValue]];

operateAns is initialized in the following way where calc is an NSMutableString:

operateAns = calc;

and I am getting the following error

'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with replaceCharactersInRange:withString:'

any ideas as to why this is happening?

1 Answers1

0

try this at time of assignment.

operateAns = [calc mutableCopy];

Ishu
  • 12,797
  • 5
  • 35
  • 51
  • This worked, but could you expand on why this must be done even though they are already mutable strings? –  Jun 10 '14 at 17:46
  • @JDodle you are taking that as NSMutableString but probably where you initialise your calc, you get NSString. you can check by debugging what kind object is calc. – Ishu Jun 11 '14 at 03:30