1、沙盒(SandBox) 數據庫
1.沙盒機制設計模式
1> 每一個應用都有屬於本身的存儲空間,即沙盒。
2> 應用只能訪問本身的沙盒,不可訪問其餘區域。
3> 若是應用須要進行文件操做,則必須將文件存放在沙盒中,尤爲是數據庫文件,在電腦上操做時,能夠去訪問,可是若是要裝在真機上可使用,必須將數據庫文件拷貝至沙盒中。
2.沙盒目錄結構api
1> Documents:在應用中創建的文件,如數據庫等能夠放在這裏,iTunes備份和恢復的時候會包括此目錄。
2> tmp:存放及時傳送的臨時文件,iTunes不會備份和恢復此目錄,此目錄下文件可能會在應用退出後刪除。
3> Library/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除。
4> Library/Preferences:應用程序偏好設置,咱們常用的NSUserDefault就保存在該目錄下的一個Plist文件中,iTnues還會同步此文件。
3.關於幾個參數數組
NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde); directory
NSSearchPathDirectory類型的enum值,代表咱們要搜索的目錄名稱(NSDocumentDirectory、NSCachesDirectory。
domainMask
NSSearchPathDomainMask類型的enum值,指定搜索範圍,這裏的NSUserDomainMask表示搜索的範圍限制於當前應用的沙盒目錄。還能夠寫成NSLocalDomainMask(表示/Library)、NSNetworkDomainMask(表示/Network)等。
expandTilde
BOOL值,表示是否展開波浪線~。咱們知道在iOS中~的全寫形式是/User/userName,該值爲YES即表示寫成全寫形式,爲NO就表示直接寫成「~」。
4.沙盒路徑的獲取緩存
#pragma mark 沙盒主路徑 -(NSString *)homePath
{ return NSHomeDirectory(); } #pragma mark 用戶應用數據路徑 +(NSString *)getDocuments
{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *path = [paths objectAtIndex:0]; return path; } #pragma mark 緩存數據路徑 +(NSString *)getCache { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); return paths[0]; } #pragma mark 臨時文件路徑 +(NSString *)getTemp { return NSTemporaryDirectory(); } #pragma mark Library路徑 +(NSString *)getLibrary { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES); NSString *path = [paths objectAtIndex:0]; return path; }
開發中常常會打印沙盒的路徑,除了用NSLog輸出以外,還能夠這樣(注意,要打斷點調試)
1.NSBundle *mainBundle = [NSBundle mainBundle];安全
bundle是一個目錄,其中包含了程序會使用到的資源. 這些資源包含了如圖像,聲音,編譯好的代碼,nib文件(用戶也會把bundle稱爲plug-in). 對應bundle,cocoa提供了類NSBundle.咱們的程序是一個bundle. 在Finder中,一個應用程序看上去和其餘文件沒有什麼區別. 可是實際上它是一個包含了nib文件,編譯代碼,以及其餘資源的目錄. 咱們把這個目錄叫作程序的main bundle。app
NSLog(@"獲取app包路徑:%@",mainBundle.bundlePath); NSLog(@"獲取app資源目錄路徑:%@",mainBundle.resourcePath); NSLog(@"應用標識bundle Identifier:%@",mainBundle.bundleIdentifier); NSLog(@"info.plist信息及其餘:%@",mainBundle.infoDictionary);
提示:關於打印字典或數組中文亂碼的問題,請自行搜索NSDitionary/NSArray + Log分類或重寫他們的-(NSString *)descriptionWithLocale:(id)locale方法。
2.Bundle的使用dom
1> 新建Bundle:既然Bundle就是一個目錄,那不妨新建一個文件夾,在其中放入咱們須要的資源素材,而後對文件夾進行重名「文件名.bundle」,以後會彈出提示框(以下圖),點擊「添加」。
2> 讀取本身的Bundle資源(以百度SDK爲例)
百度地圖的IphoneMapSdkDemo示例程序中有一個名爲」mapapi.bundle"的圖片資源包
而在其demo的「AnnotationDemoViewController.m"文件中,定義了這麼幾個宏
// 資源包文件名 #define MYBUNDLE_NAME @ "mapapi.bundle" // 拼接mapapi.bundle資源包路徑 #define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME] // 獲取mapapi.bundle資源包 #define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
// 加載資源包中規定某個文件
[[NSBundle mainBundle] pathForResource:@"XXX.png(想要獲取的文件名)" ofType:nil inDirectory:@"mapapi.bundle"];
// 加載xib文件
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil];
1.NSFileManager主要用於對目錄、文件的基本操做,且其是一個單例對象(單例模式:一種設計模式,即一個類永遠只有一個類對象),使用時NSFileManager *fm = [NSFileManager defaultManager]。函數
2.宏定義快速實現單例(前幾天在網上看到的,以爲挺好的,記錄一下)工具
1> 新建一個」Singleton.h"文件,在裏面粘貼以下代碼
// .h
#define singleton_interface(class) + (instancetype)shared##class;
// .m
#define singleton_implementation(class) \ static class *_instance; \ \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instance = [super allocWithZone:zone]; \ }); \ \ return _instance; \ } \ \ + (instancetype)shared##class \ { \ if (_instance == nil) { \ _instance = [[class alloc] init]; \ } \ \ return _instance; \ }
2> 新建一個類,分別在.h文件和.m文件裏面寫上以下圖代碼,
3> 使用:SingerTest *test = [SingerTest sharedSingerTest];
3.方法蒐集
NSString *NSUserName(void); |
返回當前登陸的用戶名 |
NSString *NSFullUserName(void); |
返回當前用戶的完整用戶名 |
NSString *NSHomeDirectory(void); |
返回當前主目錄的路徑(經常使用) |
NSString * __nullable NSHomeDirectoryForUser(NSString * __nullable userName); |
返回指定用戶名的主目錄 |
NSString *NSTemporaryDirectory(void); |
返回用於建立臨時文件夾的目錄路徑 |
NSString *NSOpenStepRootDirectory(void); |
返回當前用戶的系統根目錄 |
- (BOOL)fileExistsAtPath:(NSString *)path; |
判斷path下是否存在文件/文件夾 |
- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(nullable BOOL *)isDirectory; |
判斷path下是不是文件 |
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(nullable NSDictionary<NSString *, id> *)attributes error:(NSError **)error | 新建文件夾 參數: 1-文件夾路徑。 2-YES爲若是文件不存在,則建立;NO,文件夾不建立。 3-文件夾的屬性,可讀可寫,通常傳nil。 4-錯誤信息 。 |
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error | 移除文件/文件夾 |
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error | 複製文件/文件夾 |
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error | 移動文件/文件夾 |
- (BOOL)createFileAtPath:(NSString *)path contents:(nullable NSData *)data attributes:(nullable NSDictionary<NSString *, id> *)att |
新建文件 參數: 1-文件路徑(最後面拼接文件名) 2-須要新建的文件數據 3-屬性
|
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2; |
比較兩個path下的文件是否相同 |
- (nullable NSArray<NSString *> *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0); |
遍歷path目錄下的文件,並返回一個數組 |
- (nullable NSData *)contentsAtPath:(NSString *)path; |
獲取path下的文件數據 |
- (nullable NSArray<NSString *> *)subpathsAtPath:(NSString *)path; |
以遞歸方式獲取子項目錄列表 |
+ (NSString *)pathWithComponents:(NSArray<NSString *> *)components; |
經過一個數組建立路徑 |
pathComponents |
獲取路徑的組成部分,是一個數組 |
lastPathComponent |
路徑的最後一部分 |
pathExtension |
文件擴展名 |
- (NSString *)stringBy+<Appending/Deleting>+Path+<Component/Extension>:(NSString *)str; |
<拼接/刪除>+<路徑/後綴>名在末尾 |
1.用於針對文件的I/0操做,至關於一個文件操做手柄,能更有效的控制文件,相似C語言的文件管理。
2.使用NSFileHandle
1> 須要打開一個文件,而後獲取一個NSFileHandle對象(注意:若是這個文件不存在,則使用下列方法時不能獲取到NSFileHandle對象)
// 打開一個文件並準備讀取
NSFileHandle *fp = [NSFileHandle fileHandleForReadingAtPath:path]; // 打開一個文件並準備寫入
NSFileHandle *fp = [NSFileHandle fileHandleForWritingAtPath:path]; // 打開一個文件並準備更新(讀寫)
NSFileHandle *fp = [NSFileHandle fileHandleForUpdatingAtPath:path];
2> 對打開的文件進行I/0操做
// 寫入文件
[data WriteToFile:path atomically:YES]
3> 關閉文件(注意:在C語言中,全部操做完成以後都會關閉文件,這裏也必定要關閉,爲了保證文件的安全)
// 關閉文件
[fp closeFile];
3.NSFileHandle的重要概念:句柄(下面流程有助於瞭解句柄的做用)----具體會在後續寫NSURLConnection中用到。
給文件作一個標記,讓用戶下次寫入文件的時候從這個標記處開始存儲。而seekToEndOfFile方法則是每次存儲完後都將句柄移動至該文件末尾。
-(NSArray*)arguments //以數組的形式返回當前進程的參數
-(int)processIdentifier //返回進程標識符(進程id),用於識別每一個正在運行的進程
-(NSString*)processName //返回當前正在執行的進程名稱
-(NSString *)globallyUniqueString //每次調用這個方法時,都返回不一樣的單值字符串,能夠用這個字符串生成單值臨時文件名
-(NSString *)hostname //返回主機系統的名稱
-(NSUInteger)operatingSystem //返回表示操做系統的數字
-(NSString *)operatingSystemName //返回操做系統的名稱
-(NSString *)operatingSystemVersionString //返回操做系統的當前版本
-(void)setProcessName:(NSString *)name //將當前進程名稱設置爲name。應該謹慎地使用這個方法,應爲關於進程名稱存在一些假設(好比用戶默認的設置)