When I try to insert an object to my NSMutableArray I am getting a ‘Program received signal: SIGABRT’ error, however I don’t understand why.
Here is my code, specifically it’s the insertObject:value that is causing the error.
NSMutableDictionary *myDictionary = [NSMutableDictionary dictionary];
[myDictionary setValue:valueName.text forKey:kValueName];
[myDictionary setObject:subValuesList forKey:kSubValuesList];
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
int position = appDelegate.position;
NSMutableArray *valuesList = [[NSUserDefaults standardUserDefaults] objectForKey:kValuesArray];
[valuesList insertObject:myDictionary atIndex:position];
Values returned from
NSUserDefaultsare immutable, even if you set a mutable object as the value.You need to make a mutable copy after you retrieve the array from
NSUserDefaults. Luckily for you,NSArrayconforms to theNSMutableCopyingprotocol, so you just have to send amutableCopymessage to the array:Keep in mind that you own the object returned by
mutableCopyas per the Memory Management Rules. In other words, you need to release it when you are done with it.