SwiftyFileSystem
SwiftyFileSystem 是一个围绕 Swift 的 FileManager
类的轻量级包装框架,具有多个 URL
扩展,内置移动和复制文件时的重命名策略,常见的目录路径/URL 生成交集方法和文件大小字符串格式化。
示例
要运行示例项目,请先克隆仓库,然后从 Example 目录运行 pod install
需求
安装
SwiftyFileSystem 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile
pod 'SwiftyFileSystem'
用法
URL 扩展
import SwiftyFileSystem
// Alias constructor for URL(fileURLWithPath: "path/to/myfile")
let url = "path/to/myfile".fileURL
// URL resource properties
print(url.fileExists) // prints `true` or `false`
print(url.isLocal) // prints `true` if the file resides on the local machine
print(url.isUbiquitous) // prints `true` if the file is managed and synced the cloud
print(url.isRegularFile) // prints `true` if the file is regular file
print(url.isDirectory) // prints `true` if the file is a directory
print(url.isSymbolicLink) // prints `true` if the file is a symbolic link
print(url.isInferredHidden) // prints `true` if the filename begins wit a "." or "~" character
print(url.isHidden) // prints `true` if the file hidden by default from the user
print(url.fileSize) // prints the size of this file in bytes, if it is a regular file
print(url.sizeOfContents) // prints the recurively summated size of a directory's contents
print(url.fileCount) // prints the number of files contained in a directory
print(url.creationDate) // prints the creation date of this file
print(url.contentAccessDate) // prints the content access date of this file
print(url.modificationDate) // prints the most recent modification date of this file
查阅数据大小格式化,了解如何以格式化的字符串形式显示文件大小。
简单文件管理方法
import SwiftyFileSystem
// Enumerates and prints the file contents of a directory.
for file in FileSystem.contentsOfDirectory(at: FileSystem.mainResourcePath) {
print(file)
}
// Copy a file from one testDirectory to another, and increment the name if the
// destination file already exists using a renaming policy that puts the
// version number after the basename but before the file extension
// prefixed by a dash and bounded in parentheses.
let src = "path/to/my-src".fileURL
let dst = "path/to/my-dst".fileURL
FileSystem.copyItem(at: src, to: dst, with:
FileSystem.NamingPolicy(options:
[.versionDashed,
.versionInsideParentheses], maxRenamingAttempts: 10))
数据大小格式化
import SwiftyFileSystem
// prints file size like "##.## MB" or "##.## GB" etc."
print("path/to/myfile".fileURL.fileSize.dataSizeString(decimals: 2, format: .short))
作者
NoodleOfDeath,[email protected]
许可
SwiftyFileSystem 以 MIT 许可证可用。查阅 LICENSE 文件获取更多信息。