我正在使用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
当需要保存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];
}
产生这个输出:
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.`
如您所见,dict
中的所有对象都是属性列表值,其所有键都是NSString*
。为了执行这个,我缺少什么琐事?或者我应该放弃并使用writeToFile
或类似的?
答案 0 :(得分: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];
}
}
答案 1 :(得分: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;
}
然后你可以存档任何这样的字典(假设该方法在课堂上可用):
NSDictionary *dict = ...;
[self archive:dict withKey:@"a key of your choice"];
稍后再次检索它:
NSDictionary *dict = [self unarchiveForKey:@"a key of your choice"];
答案 2 :(得分:-2)
如果您需要存储;
- 来自自定义对象的数据
- 或自定义对象数组
您可以使用NSKeyedArchiver方法。你可以查看leviathan的answer这个方法。
但是,如果你想存储;
- 包含NSString或NSNumber的字典(类似于从JSON服务响应转换而来的字典),
- 或这种字典的数组
您不需要使用NSKeyedArchiver。您可以使用用户默认值。
在我的情况下,当我从用户默认值中检索字典时它返回nil,所以我认为NSUserDefaults不愿意保存我的字典。
但是,它已保存,但我使用了错误的getter方法从用户默认值中检索它;
[[NSUserDefaults standardUserDefaults] stringForKey:<my_key>]
请确保您使用过;
[[NSUserDefaults standardUserDefaults] dictionaryForKey:<my_key>]
或
[[NSUserDefaults standardUserDefaults] objectForKey:<my_key>]
在检查任何其他可能的原因之前。