【IOS學習基礎】檔案相關,ios學習基礎

來源:互聯網
上載者:User

【IOS學習基礎】檔案相關,ios學習基礎

 

一、沙箱(SandBox) 

  1.沙箱機制

  1> 每個應用都有屬於自己的儲存空間,即沙箱。
  2> 應用只能訪問自己的沙箱,不可訪問其他地區。
  3> 如果應用需要進行檔案操作,則必須將檔案存放在沙箱中,尤其是資料庫檔案,在電腦上操作時,可以去訪問,但是如果要裝在真機上可以使用,必須將資料庫檔案拷貝至沙箱中。

  2.沙箱目錄結構

  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輸出之外,還可以這樣(注意,要打斷點調試)
二、NSBundle

  1.NSBundle *mainBundle = [NSBundle mainBundle];

  bundle是一個目錄,其中包含了程式會使用到的資源. 這些資源套件含了像,聲音,編譯好的代碼,nib檔案(使用者也會把bundle稱為plug-in). 對應bundle,cocoa提供了類NSBundle.我們的程式是一個bundle. 在Finder中,一個應用程式看上去和其他檔案沒有什麼區別. 但是實際上它是一個包含了nib檔案,編譯代碼,以及其他資源的目錄. 我們把這個目錄叫做程式的main bundle。

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的使用

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];
 三、NSFileManager檔案管理類

  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下的檔案是否相同

 

 

 

 

        

 

 

NSPathutilities常用路徑處理方法(擴充)

- (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;

 <拼接/刪除>+<路徑/尾碼>名在末尾
            

 

 

 

 

 四、NSFileHandle檔案控制代碼類(相當於c語言的檔案File)

  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方法則是每次儲存完後都將控制代碼移動至該檔案末尾。

 

五、NSProcessInfo(瞭解)
-(NSArray*)arguments  //以數組的形式返回當前進程的參數-(int)processIdentifier  //返回進程標識符(進程id),用於識別每個正在啟動並執行進程-(NSString*)processName  //返回當前正在執行的進程名稱-(NSString *)globallyUniqueString  //每次調用這個方法時,都返回不同的單值字串,可以用這個字串產生單值臨時檔案名稱-(NSString *)hostname  //返回主機系統的名稱-(NSUInteger)operatingSystem  //返回表示作業系統的數字
-(NSString *)operatingSystemName  //返回作業系統的名稱-(NSString *)operatingSystemVersionString  //返回作業系統的目前的版本
-(void)setProcessName:(NSString *)name  //將當前進程名稱設定為name。應該謹慎地使用這個方法,應為關於進程名稱存在一些假設(比如使用者預設的設定)

 

 

    

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.