起初接触的轻量级 物理存储 方式 是 plist 可以存储 系统级别的 字典 数组 但是不能存储自定义的对象类
那会 用自定义对象做存储的 需求也不大 主要 是 还没建立面向对象意识,会的也少. 再就是真的需要存储 自定义类时候 就用了CoreData了
今天 就把落下的补上 NSCoding 轻量级 建立自定义类 存储 读写 等 常用操作
使用 NSCoding 必须遵循 <NSCoding>协议 必须实现两个方法 一个编码 一个解码 initWithCoder 是解码
//属性编码方法
- (void)encodeWithCoder:(NSCoder *)aCoder;
//属性解码方法
- (id)initWithCoder:(NSCoder *)aDecoder;
调用 + (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;会启动属性编码
调用 + (nullable id)unarchiveObjectWithFile:(NSString *)path; 会启动属性解码
不建议存在.txt文件里面 因为我用命令行 去打开模拟器里面 这个文件时候 提示"未能打开文稿"xxx.txt". 该文件格式不正确"
但是 可以解档. 直接把那个文件改为 .plist 文件 也可以打开,数据也在里面.
所以 我觉得直接存成.plist文件好了.
具体使用:
#import <UIKit/UIKit.h> @interface HFMusicModel : NSObject<NSCoding> //遵守 NSCoding 协议 @property (nonatomic, strong) NSString * img_url; @property (nonatomic, strong) NSString * amount; @property (nonatomic, strong) NSString * time; @property (nonatomic, strong) NSString * title; @property (nonatomic, strong) NSString * url; @property (nonatomic, strong) NSString * method_id; @end #import "HFMusicModel.h" @implementation HFMusicModel /** * 必须要实现的两个方法 解码 和 编码 */ //将属性进行编码 "保存 归档" - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.img_url forKey:@"img_url"]; [aCoder encodeObject:self.amount forKey:@"amount"]; [aCoder encodeObject:self.time forKey:@"time"]; [aCoder encodeObject:self.title forKey:@"title"]; [aCoder encodeObject:self.url forKey:@"url"]; [aCoder encodeObject:self.method_id forKey:@"method_id"]; } //将属性进行解码 "加载 读取 解档" - (id)initWithCoder:(NSCoder *)aDecoder //该方法也是一种 初始化 { self = [super init]; if (self) {//注意不产生死循环 self.url = [aDecoder decodeObjectForKey:@"img_url"]; self.amount = [aDecoder decodeObjectForKey:@"amount"]; self.time = [aDecoder decodeObjectForKey:@"time"]; self.title = [aDecoder decodeObjectForKey:@"title"]; self.url = [aDecoder decodeObjectForKey:@"url"]; self.method_id = [aDecoder decodeObjectForKey:@"method_id"]; } return self; }
调用
HFMusicModel *musicModel = [[HFMusicModel alloc]init]; musicModel.img_url = @"http://h.hiphotos.baidu.com/baike/pic/item/a686c9177f3e67092e15a66d3bc79f3df8dc550f.jpg"; musicModel.amount = @"1024"; musicModel.time = @"60"; musicModel.title = @"我是歌曲名称"; musicModel.method_id = @"0"; //数据写入文件 NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; NSString *path = [[NSString alloc] initWithFormat:@"%@%@",bundlePath,@"/musiclist.plist"]; BOOL isSaved = [NSKeyedArchiver archiveRootObject:musicModel toFile:path]; NSLog(@"%@",[NSNumber numberWithBool:isSaved]); //读文件数据 HFMusicModel *tempMusicModel; tempMusicModel = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; NSLog(@"%@ %@ %@ %@",tempMusicModel.title,tempMusicModel.amount,tempMusicModel.img_url,tempMusicModel.time);
也可以直接作为model 转成 NSData 存在 轻量级plist里面
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:tempMusicModel] forKey:@"musicList"]; tempMusicModel = nil; tempMusicModel = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]objectForKey:@"musicList"]]; NSLog(@"%@ %@ %@ %@",tempMusicModel.title,tempMusicModel.amount,tempMusicModel.img_url,tempMusicModel.time);
[NSUserDefaults standardUserDefaults] //存储路径 [NSUserDefaults standardUserDefaults] 存储地址在
/data/Containers/Data/Application/7490CC6B-04E8-453A-B400-CAD1202498B3/Library/Preferences/XXX.plist
[[NSBundle mainBundle] bundlePath]
//存储路径 /data/Containers/Bundle/Application/02E7AE6B-7DF7-485D-8DE6-35952D895C65/LiqunNSCodingDemo.app/musiclist.plist