我想把一个包含自定义的对象的数组写到磁盘如:User*user1=[Useralloc]init];User*user2=[Useralloc]init];NSArray*userArr=@[user1,user2];该怎么做?
2 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
你的自定义对象实现NSCoding协议,然后在.m里面实现下面两个方法-(instancetype)initWithCoder:(NSCoder*)aDecoder{self=[superinit];if(self){self.objectID=[aDecoderdecodeObjectForKey:@"objectID"];self.createdAt=[aDecoderdecodeObjectForKey:@"createdAt"];self.updatedAt=[aDecoderdecodeObjectForKey:@"updatedAt"];}returnself;}-(void)encodeWithCoder:(NSCoder*)aCoder{[aCoderencodeObject:self.objectIDforKey:@"objectID"];[aCoderencodeObject:self.createdAtforKey:@"createdAt"];[aCoderencodeObject:self.updatedAtforKey:@"updatedAt"];}再然后用那个NSKeyedArchiver就可以了
小唯快跑啊
TA贡献1863条经验 获得超2个赞
NSData*tempArchive=[NSKeyedArchiverarchivedDataWithRootObject:userArr];NSArray*arr=[NSKeyedUnarchiverunarchiveObjectWithData:tempArchive];
添加回答
举报
0/150
提交
取消