문제

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 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.

다른 팁

You have to save and retrieve the array using NSData. Possible dup.

Possible to save an integer array using NSUserDefaults on iPhone?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top