Tôi đang sử dụng KVC để tuần tự hóa một số NSObject
và cố lưu nó vào NSUserDefaults
, cho tôi Attempt to insert non-property value
khi tôi cố gắng lưu trữ NSDictionary
của mình.Tại sao NSUserDefaults không muốn lưu NSDictionary của tôi?
Sau đây là các thuộc tính của các đối tượng trong câu hỏi, 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
Khi đó là thời gian để lưu MyClass nó xảy ra ở đây:
-(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];
}
trong đó sản xuất sản lượng này:
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.`
Như bạn có thể thấy, tất cả các đối tượng trong dict
là chuyên nghiệp giá trị danh sách perty và tất cả các khóa của nó là NSString*
. Tôi đang thiếu thông tin gì để thực hiện điều này? Hoặc tôi có nên từ bỏ và sử dụng writeToFile
hoặc tương tự không?
Bạn chỉ đang in các phím. Điều gì về các giá trị? –
Đó là hoàn toàn nó, một trong những giá trị của tôi snuck trong như là một 'NSNull' (JSON' null' được sản xuất bởi 'JSONKit') và đó gần như chắc chắn là nguyên nhân. Tôi chưa làm một giải pháp. –