在過去的開發工作中試過不少檔案相關的類庫,卻總是不趁手:要麼功能過於單薄,要麼看著華麗、實際空洞。於是索性自己寫了一個,把 iOS 開發中最常遇到的檔案操作濃縮成簡潔的靜態方法。
HYFileManager 基於 NSFileManager 封裝,提供一系列靜態方法,用極少的程式碼完成日常檔案處理。它算不上完美,更像是一份工作經驗的沉澱;如果遇到問題,歡迎提 issue,我會盡快回覆。
目前它已支援 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