ios数据持久化

1、NSUserDefaults

能存储的数据类型为:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL.

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

[def setBool:YES forKey:@"default_save_bool"];

[def setFloat:1.414 forKey:@"default_save_float"];

[def setInteger:11 forKey:@"default_save_int"];

[def setObject:@"string" forKey:@"default_save_obj"];

[def synchronize];

2、plist文件

plist文件即属性列表property list,这样的文件扩展名为.plist,常用来存储一些常使用但不常改动的轻量数据,可存储类型有NSNumber,NSString,NSDate,NSData ,NSArray,NSDictionary,BOOL,这里plist文件存储可以有两种方式,一种是直接手动创建plist文件,command+n创建plist文件;一种是代码创建写入沙盒

获取文件手动路径

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];

获取沙盒路径创建plist

NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

NSString *filePath = [documentPath stringByAppendingPathComponent:@"myPlist.plist"];

如果为字典使用字典接收:

NSMutableDictionary *dic = dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

如果是数组使用数组接收:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:filePath];

写入数据

[dic setObject:@"zhangsan" forKey:@"name"];

[dic writeToFile:filePath atomically:YES];// 返回布尔值 yes代表写入成功

3、归档

归档也是iOS开发中常用的一种数据存储方式,可直接将对象存储为文件,将存储的文件转成对象,相对于NSUserDefault和plist来说,可存储自定义对象(这里对象需要遵循NSCoding协议),而且归档文件是加密的,在磁盘上看不到,更加安全可靠;归档可以分为三种:

NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

NSString *filePath = [documentPath stringByAppendingPathComponent:@"archvier.plist"];

简单归档,根据文件路径进行归档

NSArray*array =@['AA','BB','CC'];

[NSKeyedArchiver archiveRootObject:array toFile:filePath]; // 返回布尔值yes代表归档成功

根据文件路径进行解档

NSArray *arr =[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

NSData类型归档

NSMutableData *mutableData = [[NSMutableData alloc] init];    //通过data创建NSKeyedArchiver的实例对象

NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];

 //进行归档

[archiver encodeObject:@"张三"forKey:@"name"];

[archiver encodeBool:YES forKey:@"sex"];

[archiver encodeInt:25 forKey:@"age"];

[archiver finishEncoding];    //写入文件

BOOL isSuccese = [mutableData writeToFile:filePath atomically:YES];

// 解归档

NSData *data = [NSData dataWithContentsOfFile:filePath];

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

NSString*name = [unarchiver decodeObjectForKey:@"name"];

BOOLsex = [unarchiver decodeBoolForKey:@"sex"];

int age = [unarchiver decodeIntForKey:@"age"];

自定一对象类型归档,自定义类型需要遵循NSCoding协议

自定义Student类有3个属性

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *gender;

@property (nonatomic) NSInteger age;

实现两个方法

- (instancetype)initWithCoder:(NSCoder*)coder{

    if(self= [super init]) {

        _age= [coder decodeIntForKey:@"age"];

        _name= [coder decodeObjectForKey:@"name"];

        _gender= [coder decodeObjectForKey:@"gender"];

    }

    return self;

}

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

    [coder encodeInt:_age forKey:@"age"];

    [coder encodeObject:_name forKey:@"name"];

    [coder encodeObject:_gender forKey:@"gender"];

}

创建对象

Student *student = [Student alloc] init];

student.name=@"张三";

student.gender=@"男";

student.age=25;

归档:[NSKeyedArchiver archiveRootObject:student toFile:filePath];

解档:Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

4、数据库

前面三种方式都是对数据量比较小的情况下使用,而数据量比较大的情况下使用FMDB和CoreData,FMDB是针对sqlite的c语言API进行封装成oc的API,这样方便读写,省去了一些c语言的API调用带来的麻烦,而CoreData则是iOS5之后新增的Xcode自带的框架,他提供了对象、关系映射的功能,将oc对象转化成数据存储在sqlite数据库里面,省去了我们编写数据库操作命令代码。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容