質問

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