1.應用沙盒
每個iOS應用都有自己的應用沙盒,與其他文件系統隔離.應用必須待在自己的沙盒里,其他應用不能訪問該沙盒.
應用沙盒的文件系統目錄:
-Documents
-Library
-Cache
-Preferences
-tmp
-Documents:保存應用運行時生成的需要持久化的數據,iTunes同步設備時會備份該目錄.
-tmp:保存應用運行時所需的臨時數據,使用完畢后再將相應的文件從該目錄殺出,應用沒有應用時,系統也可能會清除該目錄下的文件.iTunes同步設備時不會備份該目錄.
-Library/Caches(常用):保存應用運行時生成的需要持久化的數據,iTunes同步設備時不會備份該目錄.一般存儲體積大,不需要備份的非重要數據.
-Library/Preference:保存應用的所有偏好設置,iTunes同步設備時會備份該目錄.
2.Plist存儲
應用場景:數組和字典
如何判斷一個對象能不能使用Plist?
看下有沒有writeToFile方法.
- (void)savePlist {
NSArray *arr = @[@"123",@1];
// 存儲一定要要展開路徑
NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [cachePaht stringByAppendingPathComponent:@"arr.plist"];
// File:文件的全路徑
[arr writeToFile:filePath atomically:YES];
}
- (void)readPlist {
// 存儲一定要要展開路徑
NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [cachePaht stringByAppendingPathComponent:@"arr.plist"];
NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
}
注意:
// 獲取Cache文件路徑
// NSSearchPathDirectory:搜索的目錄
// NSSearchPathDomainMask:搜索范圍 NSUserDomainMask:表示在用戶的手機上查找
// expandTilde 是否展開全路徑,如果沒有展開,應用的沙盒路徑就是~
// 存儲一定要要展開路徑
NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
Plist不能存儲自定義對象.
3.偏好設置
好處:1.不需要關心文件名
2.快速做鍵值對存儲
- (void)saveUserDefaults {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"lxx" forKey:@"account"];
[userDefaults setObject:@"123" forKey:@"pwd"];
[userDefaults setBool:YES forKey:@"rmbPwd"];
// 馬上把跟硬盤同步
[userDefaults synchronize];
}
- (void)readUserDefaults {
NSString *pwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwd"];
}
偏好設置的底層是封裝了一個字典.
4.歸檔
存儲自定義對象.
Person.h文件
// 如果一個自定義對象想要歸檔,必須遵守NSCoding協議,實現協議方法。
@interface Person : NSObject<NSCoding>
@property (nonatomic, assign) int age;
@property (nonatomic, strong) NSString* name;
Person.m文件
@implementation Person
// 什么時候調用:自定義對象歸檔的時候
// 作用:用來描述當前對象里面的哪些屬性需要歸檔
- (void)encodeWithCoder:(NSCoder *)aCoder
{
// name
[aCoder encodeObject:_name forKey:@"name"];
// age
[aCoder encodeInt:_age forKey:@"age"];
}
// 什么時候調用:解檔對象的時候調用
// 作用:用來描述當前對象里面的哪些屬性需要解檔
// initWithCoder:就是用來解析文件的。
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
// 注意:一定要給成員變量賦值
// name
_name = [aDecoder decodeObjectForKey:@"name"];
// age
_age = [aDecoder decodeIntForKey:@"age"];
}
return self;
}
外面存儲和讀取:
- (void)save {
Person *p = [[Person alloc] init];
p.age = 18;
// 獲取cache
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 獲取文件的全路徑
NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
// 把自定義對象歸檔
[NSKeyedArchiver archiveRootObject:p toFile:filePath];
}
- (void)read {
// 獲取cache
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 獲取文件的全路徑
NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
// 解檔
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}