Reputation:
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];
Upvotes: 2
Views: 1004
Reputation: 28572
Values returned from NSUserDefaults
are 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, NSArray
conforms to the NSMutableCopying
protocol, so you just have to send a mutableCopy
message to the array:
NSMutableArray *valuesList = [[[NSUserDefaults standardUserDefaults] objectForKey:kValuesArray] mutableCopy];
Keep in mind that you own the object returned by mutableCopy
as per the Memory Management Rules. In other words, you need to release it when you are done with it.
Upvotes: 7
Reputation: 7944
You have to save and retrieve the array using NSData. Possible dup.
Possible to save an integer array using NSUserDefaults on iPhone?
Upvotes: 0