我想在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