将数组对象(自定义的info对象)保存在本地时,
转换时,
1. 要转换的数据必须是要实现NSCoding持久化
2. 先在接口中实现协议<NSCoding>来支持encode和decode功能
说明:其他基本对象不需要NSCoding持久化(基本数据如NSString)
否则保存数据时会出错误
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:@(loaclBoardId)forKey:@"loaclBoardId"];
[aCoder encodeObject:self.boardNameforKey:@"boardName"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self == [superinit])
{
self.loaclBoardId = [[aDecoderdecodeObjectForKey:@"loaclBoardId"]integerValue];
self.boardName = [aDecoderdecodeObjectForKey:@"boardName"];
}
returnself;
}
数据的读
1.将_tFEditBoardGoodsInfoData从本地的文件中读取出来
2. 获取该数据的数据文件
NSMutableArray *_dataArray = [[NSMutableArrayalloc]init];
if (_tFEditBoardGoodsInfoData !=nil)
{
_dataArray = [NSKeyedUnarchiverunarchiveObjectWithData:_tFEditBoardGoodsInfoData];
}
数据的写
1.将数组文件转换成NSData数据
NSMutableArray *copeArray = [_boardInfoDataArraymutableCopy];//深拷贝数组文件
NSData *_tFEditBoardGoodsInfo = [NSKeyedArchiverarchivedDataWithRootObject:copeArray];
2.写入本地文件进行保存