문제

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