Pretty simple code, which I may say worked as intended in Xcode 4.1 but breaks in Xcode 4.2. Here is the offending code:
-(void)mergeDevData2Email:(NSMutableString *)target codeArray:(NSArray *)array1 valueArray:(NSArray *)array2 {
NSUInteger n = 0;
for (NSMutableString *aCode in array1) {
if ([array2 count] > n) {
NSMutableString *arg = [array2 objectAtIndex:(NSUInteger)n];
NSLog(@"Target isKindOf NSMutableString: %@", ([target isKindOfClass:[NSMutableString class]]) ? @"YES" :@"NO");
NSLog(@"aCode isKindOf NSMutableString: %@", ([aCode isKindOfClass:[NSMutableString class]]) ? @"YES" :@"NO");
NSLog(@"arg isKindOf NSMutableString: %@", ([arg isKindOfClass:[NSMutableString class]]) ? @"YES" :@"NO");
[target replaceOccurrencesOfString:aCode withString:arg options:NSLiteralSearch range:NSMakeRange(0, [target length])];
n++;
}
else {
break;
}
}
}
This is what the NSLogs display:
2011-11-03 15:42:59.967 TestProg[30413:c503] Target isKindOf NSMutableString: YES
2011-11-03 15:42:59.968 TestProg[30413:c503] aCode isKindOf NSMutableString: YES
2011-11-03 15:42:59.969 TestProg[30413:c503] arg isKindOf NSMutableString: YES
When I execute the [target replaceOcurances... line of code I crash with-
Program received signal: "SIGABRT".
With the following in the console log -
2011-11-03 15:43:26.828 TestProg[30413:c503] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with replaceOccurrencesOfString:withString:options:range:'
My question is, WHERE am I attempting to mutate an immutable object ? Secondarily, why did this execute just fine in Xcode 4.1 ? Certainly all players looked mutable to Xcode 4.1. What's the difference in Xcode 4.2 ? I am missing something subtle here.