Following code work fine on emulator, but on real device i got exception:

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"] == nil)
{
    NSMutableDictionary *favorites = [[NSMutableDictionary alloc] init];
    [[NSUserDefaults standardUserDefaults] setObject:favorites forKey:@"favorites"];
}

Exception on next line of code:

NSMutableDictionary *favorites = [NSMutableDictionary dictionaryWithDictionary:
    [[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"]];

Here debugger message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'*** -[NSDictionary initWithDictionary:copyItems:]: dictionary argument is not
an NSDictionary'

I use iPad device with iOS 7.1.

EDIT

On real devise the old version of the application used this key in NSUserDefaults for NSArray. Thanks for help!

有帮助吗?

解决方案

It looks like what is already stored in your user preferences is not a dictionary.

You could change the test at the beginning like this:

if (! [[[NSUserDefaults standardUserDefaults] objectForKey:@"favourites"] 
           isKindOfClass:[NSDictionary class]])

This will cover the case that there is nothing in your user defaults, or that you have an object there that isn't a dictionary.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top