iOS的幾種數(shù)據(jù)持久化方案
plist文件(屬性列表)
preference(偏好設(shè)置)
NSKeyedArchiver(歸檔)
SQLite
CoreData
沙盒目錄結(jié)構(gòu)
"應(yīng)用程序包": 這里面存放的是應(yīng)用程序的源文件定页,包括資源文件和可執(zhí)行文件耸袜。
Documents: 最常用的目錄,iTunes同步該應(yīng)用時會同步此文件夾中的內(nèi)容,適合存儲重要數(shù)據(jù)剂买。
Library/Caches: iTunes不會同步此文件夾,適合存儲體積大老玛,不需要備份的非重要數(shù)據(jù)蜕青。
Library/Preferences: iTunes同步該應(yīng)用時會同步此文件夾中的內(nèi)容,通常保存應(yīng)用的設(shè)置信息横蜒。
tmp: iTunes不會同步此文件夾胳蛮,系統(tǒng)可能在應(yīng)用沒運行時就刪除該目錄下的文件,所以此目錄適合保存應(yīng)用中的一些臨時文件丛晌,用完就刪除仅炊。
plist文件,plist文件是將某些特定的類澎蛛,通過XML文件的方式保存在目錄中抚垄。
這里先說說自定義類的歸檔和解檔
NSObject沒有遵循<NSCoding>協(xié)議,所以想要調(diào)用- (instancetype)initWithCoder:(NSCoder *)aDecoder
/- (void)encodeWithCoder:(NSCoder *)aCoder
方法需要讓自定義類遵循該協(xié)議
如果需要歸檔的類是某個自定義類的子類時,就需要在歸檔和解檔之前先實現(xiàn)父類的歸檔和解檔方法呆馁。即[super encodeWithCoder:aCoder]
和[super initWithCoder:aDecoder]
方法;使用
需要把對象歸檔是調(diào)用NSKeyedArchiver的工廠方法 archiveRootObject: toFile: 方法桐经。
需要從文件中解檔對象就調(diào)用NSKeyedUnarchiver的一個工廠方法 unarchiveObjectWithFile: 即可。代碼
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding>
@property (nonatomic, copy) NSString *dream;
@end
#import "Person.h"
@implementation Person
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
NSLog(@"initWithCoder");
_dream = [aDecoder decodeObjectForKey:@"DreamKey"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
NSLog(@"encodeWithCoder");
[aCoder encodeObject:_dream forKey:@"DreamKey"];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (nonatomic, strong) Person *boy;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person *p = [[Person alloc] init];
self.boy = p;
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[btn1 setBackgroundColor:[UIColor blueColor]];
UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(275, 0, 100, 100)];
[btn2 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[btn1 setTitle:@"歸檔" forState:UIControlStateNormal];
[btn2 setTitle:@"解檔" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(clickBtn1) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(clickBtn2) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clickBtn1{
self.person.dream = @"happy";
NSMutableArray *arr = [NSMutableArray array];
[arr addObject:self.person];
NSString *homePath = NSHomeDirectory();
NSString *path = [homePath stringByAppendingPathComponent:@"Library/Caches/hehe.archive"];
[NSKeyedArchiver archiveRootObject:arr toFile:path];
}
- (void)clickBtn2{
NSString *homePath = NSHomeDirectory();
NSString *path = [homePath stringByAppendingPathComponent:@"Library/Caches/hehe.archive"];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (arr.count) {
self.person = arr[0];
NSLog(@"%@", self.person.dream);
}
}
@end
運行結(jié)果
- 點擊解檔浙滤,啥都沒發(fā)生,
- 點擊歸檔阴挣,打印encodeWithCoder
- 退出App
- 點擊解檔,打印"initWithCoder" "happy"