我想在NSMutableDictionary中存储自定义对象。当我从NSMutableDictionary中读取对象时,保存时总是为空。在NSMutableDictionary中存储自定义对象
下面是代码
//保存
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
CustomObject *obj1 = [[CustomObject alloc] init];
obj1.property1 = @"My First Property";
[dict setObject:obj1 forKey:@"FirstObjectKey"];
[dict writeToFile:[self dataFilePath] atomically:YES];
//读取
NSString *filePath = [self dataFilePath];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
CustomObject *tempObj = [dict objectForKey:@"FirstObjectKey"];
NSLog(@"Object %@", tempObj);
NSLog(@"property1:%@,,tempObj.property1);
我怎么能存储的NSMutableDictionary自定义类的对象?
NSLog输出告诉你什么?你能读取该文件来验证你的对象是否正确写入它? – alesplin 2011-04-11 18:59:06
2011-04-11 19:31:14.386持久性[1757:207] Object(null) 2011-04-11 19:31:14.388持久性[1757:207] property1:(null) – Leo 2011-04-11 19:05:31
' - [NSDictionary writeToFile:原子地:]'返回一个'BOOL'。检查结果是否发生在你身上? – 2011-04-11 19:23:32