Resolved Simple NSMutablestring search and replace.

Discussion in 'iOS Programming' started by IDMah, Jun 2, 2014.

  1. IDMah, Jun 2, 2014
    Last edited: Jul 17, 2014

    IDMah macrumors 6502


    May 13, 2011
    this keeps giving me:

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with replaceCharactersInRange:withString:'
    *** First throw call stack:
    Yes was initialized as
    "NSMutableString *repImage;"
    in .h
    Using ARC is that why??
    Not Working. 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.
       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");
  2. chown33 macrumors 604

    Aug 9, 2009
    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:
       NSMutableString *relpacerImage = [[NSMutableString alloc]init];
                relpacerImage = [NSMutableString stringWithFormat:@"%@",self.repImage];
    can be replaced with this:
    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?

Share This Page