为什么NSUserDefaults不愿意保存我的NSDictionary?

Tho*_*mer 14 iphone objective-c nsdictionary nsuserdefaults kvc

我正在使用KVC来序列化NSObject并尝试将其保存到NSUserDefaults,这给了我一个Attempt to insert non-property value当我尝试存储我的时候NSDictionary.

以下是相关对象的属性MyClass:

@interface MyClass : NSObject
@property (copy,nonatomic) NSNumber* value1;
@property (copy,nonatomic) NSNumber* value2;
@property (copy,nonatomic) NSString* value3;
@property (copy,nonatomic) NSString* value4;
@property (copy,nonatomic) NSString* value5;
@property (copy,nonatomic) NSString* value6;
@property (copy,nonatomic) NSString* value7;
@property (copy,nonatomic) NSString* value8;
@end
Run Code Online (Sandbox Code Playgroud)

什么时候保存MyClass,它出现在这里:

-(void)saveMyClass
{
  NSArray* keys = [NSArray arrayWithObjects:
                @"value1",
                @"value2",
                @"value3",
                @"value4",
                @"value5",
                @"value6",
                @"value7",
                @"value8",
                nil];
  NSDictionary* dict = [self dictionaryWithValuesForKeys:keys];
  for( id key in [dict allKeys] )
  {
    NSLog(@"%@ %@",[key class],[[dict objectForKey:key] class]);
  }
  NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  [defaults setObject:dict forKey:[NSString stringWithString:kMyClassKey]];
  [defaults synchronize];
}
Run Code Online (Sandbox Code Playgroud)

产生这个输出:

2012-02-23 19:35:27.518 MyApp[10230:40b] __NSCFConstantString __NSCFNumber
2012-02-23 19:35:27.519 MyApp[10230:40b] __NSCFConstantString __NSCFNumber
2012-02-23 19:35:27.519 MyApp[10230:40b] __NSCFConstantString __NSCFString
2012-02-23 19:35:27.519 MyApp[10230:40b] __NSCFConstantString __NSCFString
2012-02-23 19:35:27.520 MyApp[10230:40b] __NSCFConstantString __NSCFString
2012-02-23 19:35:27.520 MyApp[10230:40b] __NSCFConstantString __NSCFString
2012-02-23 19:35:27.520 MyApp[10230:40b] __NSCFConstantString __NSCFString
2012-02-23 19:35:27.520 MyApp[10230:40b] __NSCFConstantString NSNull
2012-02-23 18:38:48.489 MyApp[9709:40b] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '{
    value1 = "http://www.google.com";
    value2 = "MyClassData";
    value3 = 8;
    value4 = "<null>";
    value5 = "http://www.google.com";
    value6 = 1;
    value7 = "http://www.google.com";
    value8 = 4SY8KcTSGeKuKs7s;
}' of class '__NSCFDictionary'.  Note that dictionaries and arrays in property lists must also contain only property values.`
Run Code Online (Sandbox Code Playgroud)

如您所见,dict属性列表值及其所有键中的所有对象都是NSString*.为了执行这个,我缺少什么琐事?或者我应该放弃使用writeToFile或类似?

Tho*_*mer 21

建议打印值的Kevin的道具,当然其中一个类型NSNull不是属性列表值.谢谢!

我问题的kludgy解决方案 - 迭代我刚刚生成的字典的键,dictionaryWithValuesForKeys并删除类型的那些NSNull.叹

NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:[self dictionaryWithValuesForKeys:keys]];
for( id key in [dict allKeys] )
{
    if( [[dict valueForKey:key] isKindOfClass:[NSNull class]] )
    {
        // doesn't work - values that are entered will never be removed from NSUserDefaults
        //[dict removeObjectForKey:key];
        [dict setObject@"" forKey:key];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,我讨厌你`JSONKit`和你!@#%@#!`NSNull`s (6认同)

lev*_*han 20

我将它们保存到用户默认值时通常会归档和取消归档词典.这样您就不必手动检查NSNull值.

只需在代码中添加以下两种方法即可.可能属于一个类别.

- (BOOL)archive:(NSDictionary *)dict withKey:(NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = nil;
    if (dict) {
        data = [NSKeyedArchiver archivedDataWithRootObject:dict];
    }
    [defaults setObject:data forKey:key];
    return [defaults synchronize];
}

- (NSDictionary *)unarchiveForKey:(NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:key];
    NSDictionary *userDict = nil;
    if (data) {
        userDict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    return userDict;
}
Run Code Online (Sandbox Code Playgroud)

然后你可以存档任何这样的字典(假设该方法在类中可用):

NSDictionary *dict = ...;
[self archive:dict withKey:@"a key of your choice"];
Run Code Online (Sandbox Code Playgroud)

并在以后再次检索它,如下所示:

NSDictionary *dict = [self unarchiveForKey:@"a key of your choice"];
Run Code Online (Sandbox Code Playgroud)