plist存储
存数据
- (IBAction)save:(id)sender {
//数据存储是保存在手机里面的
//plist文件存储一般都是存取字典和数组,直接写成plist文件,把它存到应用沙盒当中.
//只有在ios当中才有plist存储,它是ios特有的存储方式.
//获取沙盒根根路径,每一个应用在手机当中都有一个文件夹,这个方法就是获取当前应用在手机里安装的文件.
NSString *homeDir = NSHomeDirectory();
NSLog(@"homeDir = %@",homeDir);
//在某个范围内搜索文件夹的路径.
//directory:获取哪个文件夹
//domainMask:在哪个路径下搜索
//expandTilde:是否展开路径.
//这个方法获取出的结果是一个数组.因为有可以搜索到多个路径.
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//在这里,我们指定搜索的是Cache目录,所以结果只有一个,取出Cache目录
NSString *cachePath = array[0];
// 拼接文件路径
NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];
//想要把这个字典存储为plist文件.
//直接把字典写入到沙盒当中
// 用字典写, plist文件当中保存的是字典.
NSDictionary *dict = @{@"age" : @18,@"name" : @"gaowei"};
// 获取沙盒路径
//ToFile:要写入的沙盒路径
[dict writeToFile:filePathName atomically:YES];
//用数组写,plist文件当中保存的类型是数组.
NSArray *dataArray = @[@56,@"asdfa"];
//获取沙盒路径
//ToFile:要写入的沙盒路径
[dataArray writeToFile:filePathName atomically:YES];
}
读取数据
- (IBAction)reader:(id)sender {
//这个方法获取出的结果是一个数组.因为有可以搜索到多个路径.
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
// 在这里,我们指定搜索的是Cache目录,所以结果只有一个,取出Cache目录
NSString *cachePath = array[0];
//拼接文件路径
NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];
// 从文件当中读取字典, 保存的plist文件就是一个字典,这里直接填写plist文件所存的路径
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePathName];
//如果保存的是一个数组.那就通过数组从文件当中加载.
NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePathName];
NSLog(@"%@",dataArray);
}
偏好设置
- (IBAction)save:(id)sender {
//偏好设置NSUserDefaults
//底层就是封闭了一个字典,利用字典的方式生成plist文件
//好处:不需要关心文件名(它会自动生成)快速进行键值对存储.
NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults];
[defautls setObject:@"gaowei" forKey:@"name"];
[defautls setBool:YES forKey:@"isBool"];
[defautls setInteger:5 forKey:@"num"];
//同步,立即写入文件.
[defautls synchronize];
}
- (IBAction)reader:(id)sender {
//存是用什么key存的, 读的时候就要用什么key值取
//存的时候使用的什么类型,取的时候也要用什么类型.
NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
BOOL isBool = [[NSUserDefaults standardUserDefaults] boolForKey:@"isBool"];
NSInteger num = [[NSUserDefaults standardUserDefaults] integerForKey:@"num"];
NSLog(@"name =%@-isBool=%d-num=%ld",str,isBool,num);
}
归档
//保存数据
- (IBAction)save:(id)sender {
// 归档一般都是保存自定义对象的时候,使用归档.因为plist文件不能够保存自定义对象.
//如果一个字典当中保存有自定义对象,如果把这个字典写入到文件当中,它是不会生成plist文件的.
Persion *persion = [[Persion alloc] init];
persion.name = @"gaowei";
persion.age = 18;
//获取沙盒临时目录
NSString *tempPath = NSTemporaryDirectory();
NSString *filePath = [tempPath stringByAppendingPathComponent:@"persion.data"];
//archiveRootObject这个方法底层会去调用保存对象的encodeWithCoder方法,去询问要保存这个对象的哪些属性.
//所以要实现encodeWithCoder方法, 告诉要保存这个对象的哪些属性.
[NSKeyedArchiver archiveRootObject:persion toFile:filePath];
}
//读取数据
- (IBAction)reader:(id)sender {
// 获取沙盒临时目录
NSString *tempPath = NSTemporaryDirectory();
NSString *filePath = [tempPath stringByAppendingPathComponent:@"persion.data"];
// NSKeyedUnarchiver会调用initWithCoder这个方法,来让你告诉它去获取这个对象的哪些属性.
// 所以我们在保存的对象当中实现initWithCoder方法.
Persion *persion = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"name=%@---age=%d",persion.name,persion.age);
}
//要保存的对象
#import <Foundation/Foundation.h>
//要遵守<NSCoding>协议
@interface Persion : NSObject<NSCoding>
@property (nonatomic, strong) NSString *name;
@property(nonatomic, assign) int age;
@end
//archiveRootObject这个方法底层会去调用保存对象的encodeWithCoder方法,
//去询问要保存这个对象的哪些属性.
//只有遵守了NSCoding协议之后才能够实现这个方法.
-(void)encodeWithCoder:(NSCoder *)encode{
[encode encodeObject:self.name forKey:@"name"];
[encode encodeInt32:self.age forKey:@"age"];
}
//NSKeyedUnarchiver会调用initWithCoder这个方法,来让你告诉它去获取这个对象的哪些属性.
initWithCoder什么时候调用:解析一个文件的时候就会调用.
-(instancetype)initWithCoder:(NSCoder *)decoder{
//这个地方为什么没有[super initWithCoder]
//是因为它的父类没有遵守NSCoding协议
if (self = [super init]) {
//要给它里面的属性进行赋值,外界取得对象时访问该属性,里面才会用值.
self.age = [decoder decodeInt32ForKey:@"age"];
self.name = [decoder decodeObjectForKey:@"name"];
}
return self;
}
网友评论