iOS数据持久化之属性列表和归档

IOS数据持久化的方式分为三种:

1.属性列表 (自定义的Property List 、NSUserDefaults)

2.归档 (NSKeyedArchiver)

3.数据库 (SQLite、Core Data、第三方类库等)


NSUserDefaults轻量级本地数据存储

保存一个登陆界面的数据,用户名、密码之类的,可将NSUserDefaults作为首选。

NSUserDefaults存储的数据下次程序运行的时候依然存在,它把数据存储在什么地方了?如何能够清除?

其实它存储在应用程序内置的一个plist文件里,这个可以根据路径看到。

这个路径就是程序沙盒中,路径为../Library/Prefereces,里面有个plist文件,存储的就是你的userDefaults

想要删掉的话,用removeObjectForKey或者删掉沙盒。


获得NSUserDefaults单例对象

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];


使用单例对象将基本的数据类型中的数据写入文件(key)

    //BOOL/int/object(除了bool和整形/double/float,其余的类型统称为object)

    [userDefaults setBool:YES forKey:@"logIn"];

    [userDefaults setInteger:1 forKey:@"count"];

    NSArray *array = @[@"Maggie", @(19)];

    [userDefaults setObject:array forKey:@"userArray"];


如果需要马上读取存储的数据,手动调用同步的方法(立即写入沙盒)

    [userDefaults synchronize];


读取存储的数据(不同的类型,读取方法不一样;key)

    NSString *contentStr = [NSString stringWithFormat:@"logIn:%d,count:%ld, array:%@", [userDefaults boolForKey:@"logIn"], (long)[userDefaults integerForKey:@"count"], [userDefaults arrayForKey:@"userArray"]];


删除key对应的value

    [userDefaults removeObjectForKey:@"logIn"];


Plist

全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的。

Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息


将字典中的数据写入plist文件 (NSString/NSArray)

    [dic writeToFile:self.plistPath atomically:YES];


往plist写入字典中的数据

    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:self.plistPath];


读取自己创建的plist文件中的数据

    //注意:1.mainBundle获取plist文件; 2.不能写入数据

    //准备工作:手动地创建plist文件(xxx.app); 写入数据

    //获取文件的路径

    NSString *plistPath = 

    [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

    //读取数据 (Root: NSArray)

    NSArray *dataArray = 

    [[NSArray alloc] initWithContentsOfFile:plistPath];

    for (NSDictionary *dic in dataArray) {

        NSString *nameStr = [dic objectForKey:@"name"];

        NSNumber *age = [dic objectForKey:@"age"];

        NSLog(@"name:%@; age:%@", nameStr, age);

    }

    //读取数据 (Root: NSMutableDictionary)

        NSString *plistPath = 

        [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];  

        NSMutableDictionary *data = 

        [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];  

        NSLog(@"%@", data);//直接打印数据。 


归档NSKeyedArchiver

归档(又名序列化),把对象转为字节码,以文件的形式存储到磁盘上;程序运行过程中或者当再次重写打开程序的时候,可以通过解归档(反序列化)还原这些对象。


对自定义内容进行归档

  /* 

  归档的对象是Foundation框架中的对象

  带键归档一次可以存储多个对象

  归档和解归档其中任意对象都需要归档和解归档整个文件

  归档后的文件是加密的,所以归档文件的扩展名可以随意取

*/

    //获得文件路径

    //归档

    //准备工作:NSArray

    NSArray *dataArray = @[@"Jonny", @18, @[@"Objective-C", @"Ruby"]];

    //创建可变数据对象NSMutableData

    NSMutableData *mutableData = [NSMutableData data];

    NSLog(@"初始数据长度:%lu", (unsigned long)mutableData.length);

    //创建NSKeyedArchiver对象

    NSKeyedArchiver *archiver = 

    [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];

    //进行编码操作

    [archiver encodeObject:dataArray forKey:@"dataArrayKey"];

    //手动执行完成编码的操作(强制执行写操作)

    [archiver finishEncoding];

    NSLog(@"编码后的数据长度:%lu", (unsigned long)mutableData.length);

    //往文件中写

    [mutableData writeToFile:self.archivingPath atomically:YES];

    //反归档()

    //从归档文件中读取数据(NSData)

    NSData *data = [NSData dataWithContentsOfFile:self.archivingPath];

    //创建反归档类NSKeyedUnarchiver对象

    NSKeyedUnarchiver *unarchiver = 

    [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

    //解码动作(类型的确定)

    NSArray *dataArray = [unarchiver decodeObjectForKey:@"dataArrayKey"];

    //完成解码操作

    [unarchiver finishDecoding];


对自定义的对象进行归档 

    /*

      自定义的对象归档需要实现NSCoding协议,并且实现协议中的方法

      NSCoding协议中有两个方法:

          encodeWithCoder方法对对象属性进行编码,在对象归档时调用

          initWithCoder方法解码归档数据来初始化对象,在对象解归档时调用

      通过归档创建的文件是加密的

    */

    //对类中的属性进行编码操作触发的方法

- (void)encodeWithCoder:(NSCoder *)aCoder {

    [aCoder encodeObject:self.name forKey:@"nameKey"];

    [aCoder encodeInteger:self.age forKey:@"ageKey"];

}

    //对类中的属性进行解码操作的触发方法

- (id)initWithCoder:(NSCoder *)aDecoder {

    if (self = [super init]) {

        //解码操作

        self.name = [aDecoder decodeObjectForKey:@"nameKey"];

        self.age = [aDecoder decodeIntegerForKey:@"ageKey"];

    }

    return  self;

}

    //自定义对象与自定义内容归档和解归档步骤和用法完全相同

    //归档/反归档对象:自定义的Person.h/.m

    //创建两个person实例Instance

    Person *firstPerson = [[Person alloc] initWithName:@"Maggie" withAge:18];

    Person *secondPerson = [[Person alloc] initWithName:@"Jonny" withAge:19];

    //归档操作

    //可变Data

    NSMutableData *data = [NSMutableData data];

    //NSKeyedArchiving对象

    NSKeyedArchiver *archiver = 

    [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

    //Person对象进行编码 (自动地调用encodeWithCoder)

    [archiver encodeObject:firstPerson forKey:@"firstKey"];

    [archiver encodeObject:secondPerson forKey:@"secondKey"];

    //执行完成编码操作

    [archiver finishEncoding];

    //写入文件

    [data writeToFile:self.archivingPath atomically:YES];

    //反归档操作 ()

    //从归档文件中读取数据NSData

    NSData *fileData = [NSData dataWithContentsOfFile:self.archivingPath];

    //创建反归档对象

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:fileData];

    //解码操作

    Person *getFirstPerson = [unarchiver decodeObjectForKey:@"firstKey"];

    Person *getSecondPerson = [unarchiver decodeObjectForKey:@"secondKey"];

    //完成解码动作

    [unarchiver finishDecoding];






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值