99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829

Swift 文件管理框架 FileKit

發(fā)布時(shí)間:2017-02-21 22:28  回復(fù):0  查看:6441  感興趣:67  贊:1   最后回復(fù):2017-02-21 22:28  

FileKit是一個(gè)簡單且可以使用表達(dá)式來管理文件的Swift框架。

示例代碼:

路徑

通過Path結(jié)構(gòu)體創(chuàng)建路徑。

let home = Path("~")
let drive: Path = "/Volumes/Macintosh HD"
let file:  Path = "~/Desktop/file\(1)"
新建文件

可以通過在Path上調(diào)用createFile()來編寫空白文件。

try Path(".gitignore").createFile()
新建目錄

可以通過在Path上調(diào)用createDirectory()來創(chuàng)建目錄。

try Path("~/Files").createDirectory()
try Path("~/Books").createDirectory(withIntermediateDirectories: false)
創(chuàng)建符號(hào)鏈接

可以通過在Path上調(diào)用createSymlinkToPath(_:)創(chuàng)建符號(hào)鏈接。

try Path("path/to/MyApp.app").symlinkFile(to: "~/Applications")
print(Path("~/Applications/MyApp.app").exists)  // true
查找路徑

在桌面的5個(gè)層級(jí)目錄內(nèi)查找所有.txt后綴的文件。

let textFiles = Path.userDesktop.find(searchDepth: 5) { path in
    path.pathExtension == "txt"
}

使用searchDepth來指定查找的目錄層級(jí)。

迭代路徑

for download in Path.userDownloads {
    print("Downloaded file: \(download)")
}

當(dāng)前工作目錄

可以使用Path.Current更改進(jìn)程的當(dāng)前工作目錄。

要快速將當(dāng)前工作目錄更改為某個(gè)路徑,請(qǐng)使用changeDirectory(_:)方法:

Path.userDesktop.changeDirectory {
    print(Path.current)  // "/Users/nvzqz/Desktop"
}

共同祖先目錄

可以獲得兩個(gè)路徑之間的共同祖先:

print(Path.root.commonAncestor(.userHome))       // "/"
print("~/Desktop"  <^> "~/Downloads")            // "~"
print(.UserLibrary <^> .UserApplicationSupport)  // "/Users/nvzqz/Library"

+操作

追加兩個(gè)路徑并返回結(jié)果

// ~/Documents/My Essay.docx
let essay = Path.userDocuments + "My Essay.docx"
也可以把字符串拼接成路徑
let numberedFile: Path = "path/to/dir" + String(10)  // "path/to/dir/10"
+=操作

將右側(cè)的路徑添加到左側(cè)路徑。 也適用于String。

var photos = Path.userPictures + "My Photos"  // ~/Pictures/My Photos
photos += "../My Other Photos"                // ~/Pictures/My Photos/../My Other Photos
更多請(qǐng)參見開源代碼主頁。

相關(guān)開源代碼

您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?