1
如何存儲自定義對象NSMutableArray
? 我有加載和保存文件的代碼:ios存儲自定義對象
- (NSMutableArray *)loadDataFromFile:(NSString *)fileName {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *path = [docDir stringByAppendingPathComponent:fileName];
NSFileManager *fileMgr = [NSFileManager defaultManager];
if (![fileMgr fileExistsAtPath:path]) {
NSArray *fileArray = [fileName componentsSeparatedByString:@"."];
NSString *name = [fileArray objectAtIndex:0];
NSString *ext = [fileArray objectAtIndex:1];
NSString *bundle = [[NSBundle mainBundle] pathForResource:name ofType:ext];
[fileMgr copyItemAtPath:bundle toPath:path error:&error];
}
NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:path];
return data;
}
- (void)saveData:(NSMutableArray *)arrayData toFile:(NSString *)filename forKey:(NSString *)key {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *path = [docDir stringByAppendingPathComponent:filename];
[arrayData writeToFile: path atomically:YES];
NSLog(@"%@", arrayData);
}
但是,當我用data.plist
作爲文件名,它沒有工作,因爲NSLog(@"%@", arrayData);
返回列表自定義對象不會忽略:
「AreaTableRecord:0x76a7ef0」
:
此自定義對象使用此代碼插入陣列
我該如何存儲NSMutableArray
self.areas
包含自定義對象AreaTableRecord? 和 什麼文件格式shloud我用來存儲這些數據? (在我看來,plist不起作用)
thx男人你救了我的命:D – 2013-03-29 02:06:59
一個慷慨的答案!費裏,給他投票! :) – Fattie 2014-08-01 11:03:50