iOS NSCoding 的學習 和 使用
起初接觸的輕量級 物理存儲 方式 是 plist 可以存儲 系統(tǒng)級別的 字典 數組 但是不能存儲自定義的對象類
那會 用自定義對象做存儲的 需求也不大 主要 是 還沒建立面向對象意識,會的也少. 再就是真的需要存儲 自定義類時候 就用了CoreData了
今天 就把落下的補上 NSCoding 輕量級 建立自定義類 存儲 讀寫 等 常用操作
使用 NSCoding 必須遵循 <NSCoding>協(xié)議 必須實現兩個方法 一個編碼 一個解碼 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 協(xié)議 @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) {//注意不產生死循環(huán) 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(@"%@\n%@\n%@\n%@",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(@"%@\n%@\n%@\n%@",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
posted on 2016-07-14 15:52 ACM_Someone like you 閱讀(1126) 評論(0) 收藏 舉報
浙公網安備 33010602011771號