iOS數(shù)據(jù)存儲(chǔ)的常用方式
- 屬性列表(plist)歸檔
- Preference(偏好設(shè)置)
- NSKeyedArchiver歸檔(NSCoding)
- SQLite3
- Core Data
plist存儲(chǔ)
什么時(shí)候使用plist存儲(chǔ):字典或者數(shù)組
plist不能存儲(chǔ)自定義對(duì)象
- (IBAction)save:(id)sender {
Person *p = [[Person alloc] init];
// 假設(shè)存儲(chǔ)數(shù)組胶逢,存儲(chǔ)應(yīng)用沙盒
// 文件保存到文件夾
NSArray *users = @[p,@"332"];
// directory:獲取哪個(gè)文件夾 NSCachesDirectory,搜索caches文件
// domainMask:獲取哪個(gè)范圍下文件夾 NSUserDomainMask,表示在當(dāng)前用戶下去搜索
// expandTilde:是否展開全路徑
// ~/Library/Caches
//獲取Caches文件夾的路徑
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件路徑
NSString *filePath = [cachesPath stringByAppendingPathComponent:@"user.plist"];
NSLog(@"%@",cachesPath);
// File文件全路 徑
[users writeToFile:filePath atomically:YES];
}
- (IBAction)read:(id)sender {
// plist讀群罄住:之前是對(duì)象怎么存的聊浅,讀取出來也是什么數(shù)組
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件路徑
NSString *filePath = [cachesPath stringByAppendingPathComponent:@"user.plist"];
NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
}
偏好設(shè)置存儲(chǔ)
偏好設(shè)置存儲(chǔ)
NSUserDefaults
用來專門做偏好設(shè)置存儲(chǔ)-
偏好設(shè)置存儲(chǔ)優(yōu)點(diǎn):
- 不需要關(guān)心文件名城看,系統(tǒng)會(huì)自動(dòng)幫你生成一個(gè)文件名
- 快速做鍵值對(duì)的存儲(chǔ)
讀取代碼
- (IBAction)save:(id)sender {
[[NSUserDefaults standardUserDefaults] setObject:@"123" forKey:@"account"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"autoLogin"];
}
- (IBAction)read:(id)sender {
NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
BOOL autoLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"autoLogin"];
NSLog(@"%d",autoLogin);
}
歸檔
歸檔:自定義對(duì)象想要存儲(chǔ)到沙盒(文件夾)恰梢,必須通過歸檔。
自定義對(duì)象要?dú)w檔熙尉,必須要遵守NSCoding協(xié)議
- (IBAction)save:(id)sender {
Person *person = [[Person alloc] init];
person.age = 10;
person.name = @"adas";
// 獲取temp文件夾路徑
NSString *tempPath = NSTemporaryDirectory();
// 獲取Document文件夾路徑
// NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 拼接文件全路徑
NSString *filePath = [tempPath stringByAppendingPathComponent:@"person.data"];
// 歸檔的時(shí)候讓person對(duì)象調(diào)用encodeWithCoder
// 傳入多個(gè)參數(shù)臀晃,可以傳入數(shù)組
[NSKeyedArchiver archiveRootObject:person toFile:filePath];
}
- (IBAction)read:(id)sender {
// 獲取temp文件夾路徑
NSString *tempPath = NSTemporaryDirectory();
// 獲取Document文件夾路徑
// NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 拼接文件全路徑
NSString *filePath = [tempPath stringByAppendingPathComponent:@"person.data"];
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",p.name);
}
Person類實(shí)現(xiàn)
// Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding>
@property (nonatomic, assign) int age;
@property (nonatomic, strong) NSString *name;
@end
// Person.m
#import "Person.h"
// 自定義對(duì)象要?dú)w檔,必須要遵守NSCoding協(xié)議
@implementation Person
// 什么時(shí)候調(diào)用:當(dāng)自定義對(duì)象歸檔的時(shí)候調(diào)用
// 作用:告訴系統(tǒng)當(dāng)前對(duì)象哪些屬性需要?dú)w檔
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInt:_age forKey:@"age"];
}
// init初始化方法,先要初始化父類
// 什么時(shí)候調(diào)用:當(dāng)自定義對(duì)象解檔的時(shí)候調(diào)用
// 作用:告訴系統(tǒng)當(dāng)前對(duì)象哪些屬性需要解檔
// 當(dāng)解析一個(gè)文件的時(shí)候夯秃,就會(huì)調(diào)用initWithCoder
- (id)initWithCoder:(NSCoder *)aDecoder
{
#warning 什么時(shí)候調(diào)用initWithCoder初始化父類
if (self = [super init]) {
// 解檔屬性
// 解檔屬性一定要記得保存到成員屬性里
_name = [aDecoder decodeObjectForKey:@"name"];
_age = [aDecoder decodeIntForKey:@"age"];
}
return self;
}
@end
initWithCoder補(bǔ)充
#import "RedView.h"
// @interface RedView : UIView
@implementation RedView
// 解檔:肯定會(huì)調(diào)用對(duì)象initWithCoder:,做父類的初始化操作
// 什么時(shí)候調(diào)用[super initWithCoder:aDecoder]:只要父類遵守了NSCoding協(xié)議座咆,就需要調(diào)用
- (id)initWithCoder:(NSCoder *)aDecoder
{
// super -> UIView
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"%s",__func__);
}
return self;
}
@end