Reputation: 11871
I am running into this error and I am fairly new to Objective-C:
I am trying to run this code:
-(void)myCheckBoxCellDidChange:(MyCheckBoxCell *)checkBox {
CellData *cellItem = _dataSourceHelper.data[checkBox.coordinate.row.rowIndex];
if(([[cellItem actualDate] isEqualToString:@""]) && ([[cellItem finishedDate] isEqualToString:@""]))
{
[[cellItem actualDate]setString:[NSString stringWithFormat:@"%@ 8:00:00 AM",[self SetSpecialDateFormat:[NSDate date]]]];
[[cellItem finishedDate]setString:[NSString stringWithFormat:@"%@ 4:00:00 PM",[self SetSpecialDateFormat:[NSDate date]]]];
}
cellItem.selected = [checkBox checked];
[spreadSheet reloadRows:@[checkBox.coordinate.row]];
}
and I put a breakpoint at the if
condition and the next line crashes with this error:
Attempt to mutate immutable object with setString:
Why is this happening and how can I fix it?
Here is the SetSpecialDateFormat
method:
- (NSString *)SetSpecialDateFormat:(NSDate *)date
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString *theDate = [dateFormat stringFromDate:date];
dateFormat = nil;
return theDate;
}
full error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with setString:'
*** First throw call stack:
(0x26b2ffef 0x34ddfc8b 0x26b2ff35 0x26aeb26f 0xb3301 0xc33d9 0x2a30206b 0x2a1b10f9 0x2a5caf9b 0x2a17a181 0x2a17805f 0x2a1af4d9 0x2a1aeddd 0x2a184fe5 0x2a3fb8fb 0x2a1839f9 0x26af5faf 0x26af53bf 0x26af3a25 0x26a40201 0x26a40013 0x2e20d201 0x2a1e4a59 0xa75b1 0x3536baaf)
libc++abi.dylib: terminating with uncaught exception of type NSException`
This is where else its being called:
actualDate = [[NSMutableString alloc]init];
[[cellData actualDate]setString:@""];
[[cellData actualDate]setString:[object valueForKey:@"actualDate"]];
[formattedCell setActualDate:(NSMutableString*)([c.actualDate isEqualToString:@""]?@"":[self reverseStringDate:[c.actualDate substringToIndex:10]])];
-(NSString *)reverseStringDate:(NSString*)originalDateString
{
NSArray* components = nil;
components = [originalDateString componentsSeparatedByString:@"-"];
NSString *reversedString = [NSString stringWithFormat:@"%@-%@-%@",[components objectAtIndex:2],[components objectAtIndex:1],[components objectAtIndex:0]];
return reversedString;
}
Upvotes: 0
Views: 268
Reputation: 62052
[formattedCell setActualDate:(NSMutableString*)([c.actualDate isEqualToString:@""]?@"":[self reverseStringDate:[c.actualDate substringToIndex:10]])];
This is very problematic. You're simply casting whatever this long chain of junk is into a mutable string... and that's just not going to work.
First of all, unnest all of this non-sense. And then, instead of casting, lets call mutableCopy
on our NSString
object so we actually get a NSMutableString
object.
NSMutableString *dateString = [@"" mutableCopy];
if (![c.actualDate isEqualToString:@""]) {
dateString = [[self reverseStringDate:[c.actualDate substringToIndex:10]] mutableCopy];
}
[formattedCell setActualDate:dateString];
Upvotes: 2