歸檔:是把資料持久化到本地,歸檔是其中的一樣,資料庫是一種持久化的歸檔,但是歸檔有一個特點,持久化到本地的資料是經過加密.不能夠用特定的軟體讀看裡面的内容.與檔案操作最大的差別.
歸檔也叫做序列化,解檔叫反序列化.
下面執行個體是把數組歸檔和解檔:
//
// main.m
// storeDemo1
//
// Created by 5016 on 13-12-18.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import <Foundation/Foundation.h>
//歸檔-----序列化
//數組歸檔
void store()
{
//1.擷取路徑
NSString *homePath = NSHomeDirectory();
NSString *targetPath = [homePath stringByAppendingPathComponent:@"Desktop/arr.archive"];
//2.準備資料
NSArray *arr = @[@002,@555,@"dragon",@"yeah"];
//3.歸檔
BOOL success = [NSKeyedArchiver archiveRootObject:arr toFile:targetPath];
if (success) {
NSLog(@"歸檔成功");
}
else
{
NSLog(@"歸檔失敗");
}
}
//解檔-----反序列化
//數組解檔
void readstroe()
{
//1.擷取路徑
NSString *homePath = NSHomeDirectory();
NSString *targetPath = [homePath stringByAppendingPathComponent:@"Desktop/arr.archive"];
//2.解檔成為數組
NSArray *unarr = [NSKeyedUnarchiver unarchiveObjectWithFile:targetPath];
NSLog(@"new arr=%@",unarr);
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
store();
readstroe();
}
return 0;
}