題外話
今天是農歷臘月二十六啦,馬上就快過年了,其實在我們老家,今天晚上就要過大年了(我們村那的風俗就是臘月二十六過大年,年三十過小年)。過年了,新的一年離我們越來越近,真心祝愿爸媽在新的一年里身體健康,萬事都順心如意,平時不要光顧著忙活掙錢,你女兒兒子們都已經長大成人,該為自己為家庭承擔起應有的責任了。在這里也衷心祝愿天下所有的爸爸媽媽身體健康,笑口常開,好人一生平安幸福。啰嗦了兩句,趕時間的請忽略哦??
寫在前面
iOS本地數據持久化 一共有四種方法
1.SQLite數據庫
2.NSKeyedArchiver (歸檔)
3.preference (偏好設置 即NSUserDefaults)
4.CoreData(未完待續)
今天 先寫歸檔小結,后續會更新其它的。
正題
大致的分為如下幾個步驟:
-
創建相應的模型實體,遵守NScoding協議。部分代碼如下(github下載??):
#import <Foundation/Foundation.h>
@interface ContactModel : NSObject<NSCoding>
@property (strong, nonatomic)NSString *name;
@property (strong, nonatomic)NSString *mobile;
//這兩個是歸檔/解檔 某個單一的model
-(void)archiverWithContactModel:(ContactModel *)contact;
+(ContactModel *)unarchiver;
//下面這兩個是對model數組進行歸檔和解檔
+ (void)archiverWithContactList:(NSMutableArray *)contacts;
+ (NSMutableArray *)unarchiverList;
@end
上面是h文件,下面來看看m文件,主要實現initWithCoder和encodeWithCoder兩個方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:name];
_mobile = [aDecoder decodeObjectForKey:mobile];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:name];
[aCoder encodeObject:_mobile forKey:mobile];
}
-
模型數組歸檔
#pragma mark --復雜對象數組寫入文件
+ (void)archiverWithContactList:(NSMutableArray *)contacts{
//1:準備存儲數據的對象
NSMutableData *data = [NSMutableData data];
//2:創建歸檔對象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//3:開始歸檔
[archiver encodeObject:contacts forKey:@"contact"];
//4:完成歸檔
[archiver finishEncoding];
//5:寫入文件當中
BOOL result = [data writeToFile:FilePath atomically:YES];
if (result) {
NSLog(@"歸檔成功:%@",FilePath);
}else
{
NSLog(@"歸檔不成功!!!");
}
}
-
模型數組解檔
#pragma mark --- 反歸檔/反序列化/解碼/解檔 ----
+ (NSMutableArray *)unarchiverList{
NSData *myData = [NSData dataWithContentsOfFile:FilePath];
//這個判斷要加上,vc第一次加載數據源的時候,這個是讀不到東西的,若程序再往下走的話勢必會導致奔潰
if (!myData) {
return nil;
}
//創建反歸檔對象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
//反歸檔
NSMutableArray *contacts = [NSMutableArray array];
contacts = [unarchiver decodeObjectForKey:@"contact"];
//完成反歸檔
[unarchiver finishDecoding];
return contacts;
}
-
歸檔使用
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
AddContactVC *vc = [segue destinationViewController];
//block傳值
vc.addContactBlock = ^(ContactModel *contact){
[self.datalist addObject:contact];
//模型數組歸檔
[ContactModel archiverWithContactList:self.datalist];
[self.tableView reloadData];
};
}
-
解檔使用
//懶加載數據源
-(NSMutableArray *)datalist{
if (!_datalist) {
_datalist = [NSMutableArray array];
if ([ContactModel unarchiverList]) {
_datalist = [ContactModel unarchiverList];
}
}
return _datalist;
}
當調用NSKeyedArchiver的時候,系統會去調用你的encodeWithCode。當時使用NSKeyedUnarchiver,系統回去調用initWithCoder。歸檔簡單的理解就是把你的實體對象壓縮歸檔到一個文件里,相反解檔就是從你設置那個文件里面讀出之前保存的數據。
時間倉促,簡單記錄下,望見諒!詳看AdressBookDemo
補充知識
終端命令上傳本地項目到github
-
初始化本地倉庫
$ git init
$ git add .
$ git commit -m "first commit"
-
與github建立連接
登錄github創建跟項目相同名稱的資源庫
終端輸入如下命令:
$ remote add origin https://github.com/caohuoxia/AdressBookDemo.git
-
上傳到github
$ git push -u origin master
敲如如上命令之后,出現授權失敗問題,如下截圖解決ok