不废话,直接贴代码:
@property NSMutableArray *NewItems;
//初始化 本地存储文件
NSString* path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
self.storeFilename = [path stringByAppendingPathComponent:@"storefile.plist"];
//存储数据
[self.NewItems addObject:item];
[self.NewItems writeToFile:self.storeFilename atomically:YES];
//从本地文件 读取数据
NSArray* storedItems = [NSArray arrayWithContentsOfFile:self.storeFilename];
[self.NewItems addObjectsFromArray:storedItems];
初始化完成后,从本地文件读取数据,空的时候正常。增加并存入数据后,下次启动,读取数据时程序崩溃。显示:
libc++abi.dylib: terminating with uncaught exception of type NSException
自定有类对象实例item不能用这种方法进行存储, 可以通过NSKeyedArchiver转成NSdata,然后才能使用writeToFile:方法。