一、應(yīng)用沙盒
一般情況下,iOS每個應(yīng)用程序都只能訪問當(dāng)前沙盒目錄下的文件。
(一)、沙盒目錄

- Documents:保存應(yīng)用運(yùn)行時生成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時會備份該目錄。
- Library:1.Caches:保存應(yīng)用運(yùn)行時生成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時不會備份該目錄。一般存儲體積大、不需要備份的非重要數(shù)據(jù)。
2.Preference:保存應(yīng)用的所有偏好設(shè)置,iOS的Settings(設(shè)置)應(yīng)用會在該目錄中查找應(yīng)用的設(shè)置信息。iTunes同步設(shè)備時會備份該目錄。 - Temp:保存應(yīng)用運(yùn)行時所需的臨時數(shù)據(jù),使用完畢后再將相應(yīng)的文件從該目錄刪除。應(yīng)用沒有運(yùn)行時,系統(tǒng)也可能會清除該目錄下的文件。iTunes同步設(shè)備時不會備份該目錄。
(二)、獲取文件目錄的路徑
// 獲取沙盒主目錄路徑
NSString *HomeFile = NSHomeDirectory();
NSLog(@"HomeFile%@",HomeFile);
// 獲取Documents目錄路徑
NSString *DocumentsFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"DocumentsFile%@",DocumentsFile);
// 獲取Library的目錄路徑
NSString *LibraryFile = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"LibraryFile%@",LibraryFile);
// 獲取Caches目錄路徑
NSString *CachesFile = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"CachesFile%@",CachesFile);
// 獲取Tmp目錄路徑
NSString *TmpFile = NSTemporaryDirectory();
NSLog(@"TmpFile%@",TmpFile);
獲取應(yīng)用程序中資源的路徑方法
NSLog(@"bundlePath%@",[[NSBundle mainBundle] bundlePath]);
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];
NSLog(@"imagePath%@",imagePath);
UIImage *testImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
NSLog(@"%@",testImage);
NSSearchPathForDirectoriesInDomains 方法用于查找目錄,返回指定范圍內(nèi)的指定名稱的目錄的路徑集合,有三個參數(shù)
- directory,表明我們要搜索的目錄名稱。
- domainMask,指定搜索范圍,這里的NSUserDomainMask表示搜索的范圍限制于當(dāng)前應(yīng)用的沙盒目錄。
- expandTilde,返回YES路徑全部顯示,返回NO主路徑顯示~。
二、iOS應(yīng)用數(shù)據(jù)存儲的幾種方式
- XML屬性列表(plist)歸檔
- Preference(偏好設(shè)置)
- NSKeyedArchiver歸檔(NSCoding)
- SQLite3
- Core Data
(一)、XML屬性列表(plist)歸檔
所謂歸檔,是一個過程,即用某種格式保存一個或多個對象,以便以后還原這些對象。
屬性列表是一種XML格式的文件。如果對象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,就可以使用writeToFile:atomically:方法直接將對象寫到屬性列表文件中。
**plist歸檔
// 獲取Documents目錄路徑
NSString *DocumentsFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"DocumentsFile%@",DocumentsFile);
//獲取目錄下的plist文件
NSString *filePath = [DocumentsFile stringByAppendingPathComponent:@"date.plist"];
NSDictionary *testDict = [NSDictionary dictionaryWithObjects:@[@"1", @"2", @"3"] forKeys:@[@"test1", @"test2", @"test3"]];
[testDict writeToFile:filePath atomically:YES];

**解檔
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@-%@", key, obj);
}];
運(yùn)行結(jié)果:

(二)、偏好設(shè)置
Preference主要用到NSUserDefaults, NSUserDefaults是一個單例,提供了一個默認(rèn)的系統(tǒng)編程接口進(jìn)行交互,一般用來存儲應(yīng)用程序相關(guān)的偏好設(shè)置,配置數(shù)據(jù)(比如,保存用戶名、字體大小)等,以便于下次啟動程序之后能恢復(fù)上次的設(shè)置。在運(yùn)行時,可以使用NSUerDefaults對象讀取應(yīng)用程序的默認(rèn)數(shù)據(jù)庫來獲取數(shù)據(jù)。
NSUerDefaults具有緩存機(jī)制,所以不必再每次讀取數(shù)據(jù)的時候都打開應(yīng)用程序的默認(rèn)設(shè)置數(shù)據(jù)庫??梢酝ㄟ^synchronize方法來使內(nèi)存中的緩存與系統(tǒng)默認(rèn)數(shù)據(jù)庫進(jìn)行同步。
NSUserDefaults支持的數(shù)據(jù)類型有:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL
NSUserDefaults 本身不支持自定義對象的存儲, 但是NSUserDefaults可以存儲NSData類型,所以在存儲自定義類型時,轉(zhuǎn)換成NSData類型來存儲。
存入
//1.獲得NSUserDefaults文件
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//2.向文件中寫入內(nèi)容
[userDefaults setObject:@"小明" forKey:@"name"];
[userDefaults setInteger:21 forKey:@"age"];

讀取
NSString *name = [userDefaults objectForKey:@"name"];
NSInteger age = [userDefaults integerForKey:@"age"];
//自定義對象存取
1.創(chuàng)建studentmodel類 要遵循NSCoding協(xié)議,并實現(xiàn)協(xié)議方法
//StudentModel.h文件
#import <Foundation/Foundation.h>
@interface StudentModel : NSObject<NSCoding>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
//StudentModel.h文件
#import "StudentModel.h"
@implementation StudentModel
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
_name = [aDecoder decodeObjectForKey:@"name"];
_age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
//使用
StudentModel *model = [[StudentModel alloc]init];
model.name = @"小紅";
model.age = 18;
//存
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:data forKey:@"student"];
//取
NSData *getData = [userDefaults objectForKey:@"student"];
StudentModel *newModel = [NSKeyedUnarchiver unarchiveObjectWithData:getData];
NSLog(@"%@,%ld",newModel.name,newModel.age);
如果沒有調(diào)用synchronize方法,系統(tǒng)會根據(jù)I/O情況不定時刻地保存到文件中。所以如果需要立即寫入文件的就必須調(diào)用synchronize方法。
[userDefaults synchronize];
偏好設(shè)置會將所有數(shù)據(jù)保存到同一個文件中。即preference目錄下的一個以此應(yīng)用包名來命名的plist文件。
(三)、NSKeyedArchiver歸檔(NSCoding)
如果對象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,可以直接用NSKeyedArchiver進(jìn)行歸檔和恢復(fù)。
不是所有的對象都可以直接用這種方法進(jìn)行歸檔,只有遵守了NSCoding協(xié)議的對象才可以。
- 存取array類型數(shù)據(jù)
NSString *filePath = [DocumentsFile stringByAppendingPathComponent:@"arrayTeat.plist"];
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",nil];
//存
[NSKeyedArchiver archiveRootObject:array toFile:filePath];
//取
NSArray *getarray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",getarray);
- 存取自定義類型數(shù)據(jù)
StudentModel *model = [[StudentModel alloc]init];
model.name = @"小紅";
model.age = 18;
NSString *filePath = [DocumentsFile stringByAppendingPathComponent:@"studentModel.plist"];
//存
[NSKeyedArchiver archiveRootObject:model toFile:filePath];
//取
StudentModel *newModel = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@,%ld",newModel.name,newModel.age);