iOS 对象归档(1) 对foundation中对象进行归档

来源:互联网 发布:usb网络共享 无法上网 编辑:程序博客网 时间:2024/05/08 21:33

MainController.h

/* 数据持久化是通过文件将数据存储在磁盘上 四中数据持久化方式: 1.属性列表(Property List) 2.对象归档 3.SQLite 4.CoreDate  对比: 1.属性列表、对象归档适合小数据量存储和查询操作 2.SQLite、CoreDate适合大数据量存储和查询操作  对象归档: 对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径 读取文件的内容(也称为接档,反序列化)。 (对象归档的文件是保密的,在磁盘上无法查看文件中的内容,而属性列表是明文的,可以查看) 对象归档有两种方式:1:对foundation中对象进行归档  2:自定义对象归档  */#import <UIKit/UIKit.h>@interface MainController : UIViewController@end

MainController.m

#import "MainController.h"@interface MainController ()@end@implementation MainController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    //获取根目录    NSString *homeDirectory=NSHomeDirectory();    NSString *path=[homeDirectory stringByAppendingFormat:@"test.archiver"];    NSArray *array=@[@"abc",@"123",@"def",@"wto"];    BOOL flag=[NSKeyedArchiver archiveRootObject:array toFile:path];    if (flag) {        NSLog(@"归档成功");    }        //读取的文档内容    NSArray *array2=[NSKeyedUnarchiver unarchiveObjectWithFile:path];    NSLog(@"归档数据:%@",array2);            }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


原创粉丝点击