ios 关于临时数据缓存在本地/沙盒/归档/文件写入/缓存
IOS的应用在手机上至少存在两个路径,一个是程序根目录,另一个是称作沙盒的文件夹,我们要存储的数据一般放在沙盒目录中。这个目录可以通过全局方法:
NSHomeDirectory()
取得,各文件的作用如下:
① Plist文件的读写操作(在本示例里我们写入到Documents目录下):
/** * 写plist */ - (IBAction)writePlist:(id)sender { // 取得沙河中的路径 NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; // 新增文件路径(在后续的操作中如发现没有,会自动创建) NSString *dataPath = [docPath stringByAppendingPathComponent:@"data.plist"]; // 创建数据(生成的是plist文件,数据类型有所限制) NSArray *data = @[ @{@"01": @"张三", @"02": @"李四", @"03": @"王五"}, @[@1, @2, @3] ]; // 文件写入 [data writeToFile:dataPath atomically:YES]; } /** * 读plist */ - (IBAction)readPlist:(id)sender { // 取得沙河中的路径(直接拼接) NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data.plist"]; // 取得数据 NSArray *data = [NSArray arrayWithContentsOfFile:dataPath]; NSLog(@"%@", data); }
② 偏好设置的读写操作(其本身是Plist读写的封装,他只能写入到Preferences文件夹):
/** * 写入设置项 */ - (IBAction)writeSettings:(id)sender { [[NSUserDefaults standardUserDefaults] setObject:@"123" forKey:@"password"]; [[NSUserDefaults standardUserDefaults] setObject:@12 forKey:@"appleCount"]; [[NSUserDefaults standardUserDefaults] setObject:@YES forKey:@"AreYouOk"]; [[NSUserDefaults standardUserDefaults] synchronize]; // 更新 } /** * 读取设置项 */ - (IBAction)readSettings:(id)sender { NSString *v1 = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"]; NSInteger v2 = [[NSUserDefaults standardUserDefaults] integerForKey:@"appleCount"]; BOOL v3 = [[NSUserDefaults standardUserDefaults] boolForKey:@"AreYouOk"]; NSLog(@"密码为:%@,苹果%ld 个,你好吗:%d", v1, v2, v3); }
③ 归档与反归档(可以用来写入OC对象)
a、首先定义一个对象,这个对象要遵循 NSCoding协议才能进行归档和反归档操作:
头文件:
#import <Foundation/Foundation.h> @interface QXData : NSObject <NSCoding> @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *icon; @property (nonatomic, copy) NSString *detail; @end
实现文件:
#import "QXData.h" @implementation QXData // 反归档方法 - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.name = [aDecoder decodeObjectForKey:@"user_name"]; self.icon = [aDecoder decodeObjectForKey:@"user_icon"]; self.detail = [aDecoder decodeObjectForKey:@"user_detail"]; } return self; } // 归档方法 - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.name forKey:@"user_name"]; [aCoder encodeObject:self.icon forKey:@"user_icon"]; [aCoder encodeObject:self.detail forKey:@"user_detail"]; } // 方便输出 - (NSString *)description { return [NSString stringWithFormat:@"QXData: %@ - %@ - %@", self.name, self.icon, self.detail]; } @end
b、归档与反归档操作:
/** * 归档 */ - (IBAction)achiver:(id)sender { // 创建对象 QXData *d1 = [QXData new]; d1.name = @"AngryBird"; d1.icon = @"1.png"; d1.detail = @"A wonderful game!"; // 创建或取得数据路径(后缀名随便) NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data.qx"]; // 归档 [NSKeyedArchiver archiveRootObject:d1 toFile:dataPath]; } /** * 反归档 */ - (IBAction)unAchiver:(id)sender { // 创建或取得数据路径(后缀名随便) NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data.qx"]; // 反归档 QXData *d1 = (QXData *)[NSKeyedUnarchiver unarchiveObjectWithFile:dataPath]; NSLog(@"%@", d1); }
注:文件路径取得的另一种方式:
// 搜索地址(搜索路径、根目录、是否展开) NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docPath = [pathArr lastObject];