Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

IDMah

macrumors 6502
Original poster
May 13, 2011
316
11
this keeps giving me:

Code:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with replaceCharactersInRange:withString:'
*** First throw call stack:
Yes was initialized as
Code:
"NSMutableString *repImage;"
in .h
Using ARC is that why??
Not Working. code:
Code:
            if ([self.repImage rangeOfString:@"T"].location != NSNotFound) {
                NSRange foundT = [ self.repImage rangeOfString:@"T"];
                [self.repImage replaceCharactersInRange:foundT withString:@"10"];
               // NSLog(@"replacing T replacing with 10");
            }

*** OK Found it. Working code here but .. seems like a lot of trouble to replace a few strings. Can someone explain why, I needed to do all this??
or is there an easier/better solution??

This feels messy/ugly.
Code:
   NSMutableString *relpacerImage = [[NSMutableString alloc]init];
            relpacerImage = [NSMutableString stringWithFormat:@"%@",self.repImage];
            
            if ([relpacerImage rangeOfString:@"T"].location != NSNotFound) {
                NSRange foundT = [relpacerImage rangeOfString:@"T"];
                [relpacerImage replaceCharactersInRange:foundT withString:@"10"];
                self.repImage= [NSMutableString stringWithFormat:@"%@",relpacerImage];
                NSLog(@"save X replacing with Y");
            }
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
One possible cause is that something else that ran earlier has replaced the repImage object with an NSString.

Nothing you posted in your non-working code needs an NSMutableString except the replaceCharactersInRange:withString:. Therefore, the logical conclusion is that something else has made repImage immutable.

What attributes does repImage have? If 'copy' is one of them, that's probably wrong.


As a separate issue, one reason your revised code is messy/ugly is because of how you make mutable strings. This:
Code:
   NSMutableString *relpacerImage = [[NSMutableString alloc]init];
            relpacerImage = [NSMutableString stringWithFormat:@"%@",self.repImage];
can be replaced with this:
Code:
NSMutableString *relpacerImage = [self.repImage mutableCopy];
Also, since relpacerImage is already an NSMutableString after the replacement has been done, it's silly to repeat the manufacturing of yet another NSMutableString to assign to repImage. You have an NSMutableString that has the correct contents. Simply assign it to the repImage property, and it should work. If it doesn't, then something else is breaking things, because there's no logical reason it should fail. Logically, you just finished replacing characters in a unique new NSMutableString. Why wouldn't assignment work? Why would copying be necessary?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.