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