您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

HYFileManager:讓 iOS 檔案操作變簡單的工具類庫

編輯:iOS 開發基礎

在過去的開發工作中試過不少檔案相關的類庫,卻總是不趁手:要麼功能過於單薄,要麼看著華麗、實際空洞。於是索性自己寫了一個,把 iOS 開發中最常遇到的檔案操作濃縮成簡潔的靜態方法。

HYFileManager 基於 NSFileManager 封裝,提供一系列靜態方法,用極少的程式碼完成日常檔案處理。它算不上完美,更像是一份工作經驗的沉澱;如果遇到問題,歡迎提 issue,我會盡快回覆。

功能覆蓋範圍

  • 沙盒常用目錄路徑獲取,一行語法搞定;
  • 資料夾遍歷,支援深度遍歷與淺度遍歷兩種模式;
  • 檔案屬性獲取,可讀單一屬性或一次取回全部屬性;
  • 建立檔案與資料夾,可指定是否覆蓋、是否寫入預設內容;
  • 刪除檔案與資料夾,並內建清空 Caches 與 tmp 的快捷方法;
  • 複製、移動檔案與資料夾,可選擇是否覆蓋;
  • 根據路徑取得檔名與副檔名;
  • 判斷檔案/資料夾是否存在、是否為空、是檔案還是目錄,以及是否可讀可寫;
  • 取得檔案或資料夾大小,提供 NSNumber 與 NSString 兩種回傳形式;
  • 寫入檔案內容,支援基本資料型別、NSData、UIImage 與 NSCoding 物件。

目前它已支援 CocoaPods,當然也可以直接下載原始碼拖進專案。若有更好的優化建議,歡迎送 Pull Request。

使用範例

取得沙盒目錄

/* 支援的捷徑:
   + (NSString *)homeDir;
   + (NSString *)documentsDir;
   + (NSString *)libraryDir;
   + (NSString *)preferencesDir;
   + (NSString *)cachesDir;
   + (NSString *)tmpDir;
*/
NSString *homePath = [HYFileManager homeDir];

遍歷資料夾

/* 支援的捷徑:
   + (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep;
   + (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep;
   + (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep;
   + (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep;
   + (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep;
   + (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep;
*/
NSArray *libraryArr = [HYFileManager listFilesInLibraryDirectoryByDeep:NO];

取得檔案屬性

// 取得檔案建立時間
NSDate *date = (NSDate *)[HYFileManager attributeOfItemAtPath:path
                                                    forKey:NSFileCreationDate
                                                     error:&error];

此外還提供 attributesOfItemAtPath: 一次取回完整屬性字典。

建立檔案與資料夾

// 在 library 下建立 test 資料夾
NSString *directoryPath = [NSString stringWithFormat:@"%@/test", [HYFileManager libraryDir]];
BOOL isSuccess = [HYFileManager createDirectoryAtPath:directoryPath];

建立檔案時可搭配 overwrite: 與 content: 參數,控制覆蓋行為與初始內容。

刪除

// 刪除 library 下的 test 資料夾
NSString *directoryPath = [NSString stringWithFormat:@"%@/test", [HYFileManager libraryDir]];
BOOL isSuccess = [HYFileManager removeItemAtPath:directoryPath];

// 一鍵清理
[HYFileManager clearCachesDirectory];
[HYFileManager clearTmpDirectory];

複製與移動

NSError *error;
NSString *path   = [NSString stringWithFormat:@"%@/test/hyyy", [HYFileManager libraryDir]];
NSString *toPath = [NSString stringWithFormat:@"%@/hyyy", [HYFileManager libraryDir]];
BOOL isSuccess = [HYFileManager copyItemAtPath:path toPath:toPath overwrite:YES error:&error];

移動用法與複製幾乎相同,換成 moveItemAtPath:toPath:overwrite:error: 即可。

取得檔名與副檔名

NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]];
NSString *fileName = [HYFileManager fileNameAtPath:path suffix:YES];
NSString *suffix   = [HYFileManager suffixAtPath:path];

判斷存在、判空與權限

NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]];
BOOL isExist = [HYFileManager isExistsAtPath:path];
BOOL isEmpty = [HYFileManager isEmptyItemAtPath:path];
BOOL isDir   = [HYFileManager isDirectoryAtPath:path];
BOOL isRead  = [HYFileManager isReadableItemAtPath:path];
BOOL isWrite = [HYFileManager isWritableItemAtPath:path];

取得檔案大小

NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]];
NSNumber *size = [HYFileManager sizeOfFileAtPath:path error:&error];
// 若需要易讀格式,用 sizeFormattedOfFileAtPath: 即可

寫入內容

NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]];
BOOL isSuccess = [HYFileManager writeFileAtPath:path content:@"Hello World" error:error];

結語

HYFileManager 仍有不少值得改進的地方。如果你覺得它有用,歡迎給個 star;如果有任何建議,也歡迎提 issue,我會盡快跟進。

專案地址:https://github.com/castial/HYFileManager

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved