我试着去我的NSMutableArray保存到磁盘,阵列 看起来:为什么我的NSMutableArray不能保存
self.tableData = [[NSMutableArray alloc] initWithObjects:
[[Cell alloc] initWithName:@"dawdw" andImage:@"dwddw" andDescription:@"dawdw" andTypes:@"dawwd dawwd" andforWho:@"dwaadw"],
[[Cell alloc] initWithName:@"Kabanos" andImage:@"spodwwdwdrt.jpg" andDescription:@"dwdw" andTypes:@"dwdw dww" andforWho:@"dawwd"],
[[Cell alloc] initWithName:@"dwwd" andImage:@"dwwd" andDescription:@"dwwd" andTypes:@"wdwd daww" andforWho:@"dadawwa"],nil];
//execution
[self writeToPlist:@"fav.txt" withData:self.tableData];
- (void) writeToPlist: (NSString*)fileName withData:(NSMutableArray *)data
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:fileName];
[data writeToFile:finalPath atomically: YES];
}
//loading
- (NSMutableArray *) readFromPlist: (NSString *)fileName {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:fileName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:finalPath];
if (fileExists) {
NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:finalPath];
return arr;
} else {
return nil;
}
}
//load
self.tableData = [self readFromPlist:@"fav.txt"];
和可悲的是最后资料表是空的,我试过一些方法来节省NSMutableArray的,但我不能弄清楚如何。
阅读'的NSArray将writeToFile文档:原子:'。看看它可以保存的对象的类型。 – rmaddy
所以任何其他想法保存和阅读nsmutablearray? – karek
你在做什么只是保存和读取数组。问题是数组中的数据。要么更改数组数据以便可保存,要么搜索保存包含自定义对象的数组。 – rmaddy