如何存储NSMutableArray
自定义对象?我有这个用于加载和保存文件的代码:
- (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);
返回列表自定义对象地址:
“区域表记录:0x76a7ef0”
使用以下代码将此自定义对象插入到数组中:
AreaTableRecord *area=[[AreaTableRecord alloc] init];
area.title=title;
area.lastScore=0;
area.vocabulary=[[NSMutableArray alloc] init];
[self.areas addObject:area];
如何存储NSMutableArray
self.areas
包含自定义对象 AreaTableRecord?我应该使用什么文件格式来存储这些数据?(在我看来 plist 在这种情况下不起作用)