iOS中的数据持久化分为五种:
1.NSUserdefaults
2.WriteToFile(写plist文件)
3.归档(对象序列化与反序列化)
4.数据库
5.Core Data
第一种、NSUserdefaults
- (void)userDefalutsTest{
// nsuserdefaults 轻量级的存储的方式
// 一般用于存放用户名密码
NSString *homePath=NSHomeDirectory();
NSLog(@"====%@",homePath);
// NSUserDefaults 单例类
// 创建对象
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// 存值 默认会生成plist文件 在library/preference 路径下
[userDefaults setObject:self.name.text forKey:@"name"];
[userDefaults setObject:self.psw.text forKey:@"psw"];
// 可以存放很多类型
// [userDefaults setFloat:<#(float)#> forKey:<#(NSString *)#>];
// [userDefaults setInteger:<#(NSInteger)#> forKey:<#(NSString *)#>];
// [userDefaults setBool:<#(BOOL)#> forKey:<#(NSString *)#>];
// 取值
NSString *name = [userDefaults objectForKey:@"name"];
NSString *psw = [userDefaults objectForKey:@"psw"];
// 如果存放的是基本数据类型 需要调用方法进行转换
// float a = [[userDefaults objectForKey:@"float"] floatValue];
}
第二种、WriteToFile(写plist文件)
NSArray、NSDictionary、NSData、NSString这四种可以使用writetofile方法
plist文件中只能存放以下七种数据类型 arr dic data date bool number string
- (void)writeToFileTest{
// 第二种存储文件 写plist
NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"bmw",@"car", nil];
NSArray *array = [[NSArray alloc] initWithObjects:array1,dic,nil];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//NSString *filePath = [path stringByAppendingPathComponent:@"test.plist"];
NSString *filePath = [NSString stringWithFormat:@"%@/test.plist",path];
//为了线程安全
[array writeToFile:filePath atomically:YES];
//数组 字典 数据 字符串
//NSArray
//NSDictionary
//NSData
//NSString
//以上四种可以使用writetofile方法
//plist文件中只能存放以下七种数据类型
// arr dic data date bool number string
NSString *string = @"helloworld";
NSString *filePath1 = [NSString stringWithFormat:@"%@/test1.txt",path];
[string writeToFile:filePath1 atomically:YES encoding:NSUTF8StringEncoding error:nil];
//------华丽的分割线-------
//读数据
//1.读取工程文件
NSBundle *bundle = [NSBundle mainBundle];
NSString *bundlePath = [bundle pathForResource:@"test" ofType:@"plist"];
//通过路径初始化数组对象
NSArray *bundleArray = [[NSArray alloc] initWithContentsOfFile:bundlePath];
NSLog(@"==== %@",bundleArray);
//2.获取沙盒中的文件
NSString *sandBoxPath = [self filePath];
NSArray *sandBoxArray = [[NSArray alloc] initWithContentsOfFile:sandBoxPath];
NSLog(@"sandBoxArray == %@",sandBoxArray);
}
- (NSString *)filePath{
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.plist"];
return filePath;
}
第三种、归档(对象序列化与反序列化)
由于wrotetofile是有使用限制的,那我们想存对象了怎么办呢?这里就用到了对象的序列化与反序列化操作,我们这里的对象是需要遵循nscoding协议并且实现协议方法的。
@interface People : NSObject
@property NSString *name;
@property int phoneNum;
@end
// 实现nscoding协议方法
//编码
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
NSString *string = [NSString stringWithFormat:@"%d",self.phoneNum];
[aCoder encodeObject:string forKey:@"phoneNum"];
}
//解码
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self){
self.name = [aDecoder decodeObjectForKey:@"name"];
self.phoneNum = [[aDecoder decodeObjectForKey:@"phoneNum"] intValue];
}
return self;
}
//array -- data -- 写入到沙盒
- (void)archiverTest{
// 对象序列化
People *p = [[People alloc] init];
p.name = @"jack";
p.phoneNum = 521;
_array = @[p];
// [_array writeToFile:[self filePath] atomically:YES];
// 为什么七种类型可以写到plist文件中 因为遵循了nscoding协议
// 1.遵循nscoding协议
// 2.实现协议方法
// 创建一个可变data
// NSMutableData *data = [NSMutableData data];
// NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//
//// 把数组进行编码
// [archiver encodeObject:_array];
// NSLog(@" ===== %@ ",data);
//// 结束编码
// [archiver finishEncoding];
[NSKeyedArchiver archiveRootObject:_array toFile:[self filePath]];
// 写入到沙盒中
// [data writeToFile:[self filePath] atomically:YES];
}
// data -- 解码 -- array
- (void)unArchiverTest{
// 在沙盒中获取数据
// NSData *data = [[NSData alloc] initWithContentsOfFile:[self filePath]];
//
// NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//
//// 解码
// NSArray *array = [unarchiver decodeObject];
//
// NSLog(@"=====%@",array);
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePath]];
NSLog(@"====%@",array);
}
这里先介绍以上三种数据持久化方式。