Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving NSMutableArray to NSUserDefaults using NSKeyedArchiver

I'm having trouble retrieving a saved NSMutableArray containing a custom object. The app crashes and the console reports http://pastie.org/1226822. Here is my objects .h file http://pastie.org/1226823. Here is my objects .m file http://pastie.org/1226826. Here is how I save my data http://pastie.org/1226830. Here is how I retrieve my data http://pastie.org/1226831. Thanks in advance.

like image 386
enbr Avatar asked May 24 '26 21:05

enbr


1 Answers

Fixed. I used Brad Larson's code at Storing custom objects in an NSMutableArray in NSUserDefaults. I think that there was a problem with how I added the data back into my array, but it works now.

NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedArray"];
if (dataRepresentingSavedArray != nil)
{
    NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
    if (oldSavedArray != nil)
            objectArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
    else
            objectArray = [[NSMutableArray alloc] init];
}
like image 195
enbr Avatar answered May 26 '26 12:05

enbr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!