Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NSUserDefaults返回NSArray而不是NSDictionary。显然是从iOS 9.0开始_Ios_Objective C - Fatal编程技术网

NSUserDefaults返回NSArray而不是NSDictionary。显然是从iOS 9.0开始

NSUserDefaults返回NSArray而不是NSDictionary。显然是从iOS 9.0开始,ios,objective-c,Ios,Objective C,在我的应用程序中的登录过程中,我循环通过服务器响应,并将一些数据保存在NSUserDefaults中: NSMutableArray *unitTypeArray = [[NSMutableArray alloc] init]; [unitTypeArray addObject:@{@"Name":@"Item", @"Description":@"Item",

在我的应用程序中的登录过程中,我循环通过服务器响应,并将一些数据保存在NSUserDefaults中:

NSMutableArray *unitTypeArray = [[NSMutableArray alloc] init];

[unitTypeArray addObject:@{@"Name":@"Item",
                                   @"Description":@"Item",
                                   @"ID":@"0",
                                   @"Time":@"2014-12-29 12:00:00",
                                   @"AddedByUserID":@"2",
                                   @"Depth":@"0",
                                   @"Height":@"0",
                                   @"Width":@"0",
                                   @"softLimit":@"10"}];

for (NSMutableDictionary *unitType in response) {
    [unitType setValue:@10 forKey:@"softLimit"];
    [unitTypeArray addObject:unitType];
}

[[NSUserDefaults standardUserDefaults] setObject:unitTypeArray forKey:@"unitTypes"];
稍后我将检索此数组:

self.unitTypeArr = [[self.preferences objectForKey:@"unitTypes"] mutableCopy];

但我要拿回一本词典。这在iOS<9.0中运行得非常好,但由于iOS 9.0,我的应用程序崩溃了。有解决方法吗,还是我必须将NSDictionary转换为NSArray?

您正在将unitTypeArray保存到NSUserDefault。正确的?因为它是类型NSMutableArray;因此,您将从首选项中获得数组。你为什么期待《词典》

通过这种方式检索数据

NSMutableArray *unitTypeArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"unitTypes"];

我找到了一个办法让它继续下去。我问题中的第一个代码块只有在数据以前没有检索到的情况下才运行。我不明白为什么,但如果我每次登录时都运行这个,它就可以工作了。即使它包含完全相同的数据。

崩溃消息是什么?崩溃消息是:[\uu NSCFDictionary objectAtIndex:]:发送到实例的无法识别的选择器是否可以发布更多代码?例如,什么是自我偏好?只是对[NSUserDefaults standardUserDefaults]的缓存引用?引用objectAtIndex的代码是什么?unitTypeArray是一个字典对象数组,这一点可能很重要——也许您正试图从该数组中读取对象,而不是从数组本身读取对象?您确定没有其他东西在为“unitTypes”设置值吗?你能展示一个完整的、最少的代码示例吗?上面的代码看起来不错。您正在从其他位置设置
unitTypes
,或者错误没有在其他位置发生。什么是
self.preferences
?如果将
self.preferences
替换为
[NSUserDefaults standardUserDefaults]
,会发生什么情况?因为我要拿回NSDictionary:!。