解析常用路径,包括项目、可执行文件和工作目录。
最新版本的 FileKit
(0.x.x) 可以在 Swift 5.2 及更高版本上运行,支持 macOS 和 Ubuntu Linux。
将 FileKit
添加到应用程序 Package.swift
文件中的依赖项。将 "x.x.x"
替换为最新的 FileKit
版本。
.package(url: "https://github.com/Kitura/FileKit.git", from: "x.x.x")
将 FileKit
添加到你的目标依赖项
.target(name: "example", dependencies: ["FileKit"]),
import FileKit
如果处理 URL,你还需要导入 Foundation
包
import Foundation
指向包含项目可执行文件的目录。
例如,当在 Xcode 中运行名为 MySwiftProject
的可执行文件时,可执行文件目录字符串将为 "/Users/username/MySwiftProject/.build/debug";当从命令行运行同一项目时,它将为 "/Users/username/MySwiftProject/.build/x86_64-apple-macosx10.10/debug"。
/// Executable Folder String
let stringUrl = FileKit.executableFolder
/// Executable Folder URL
let urlObject = FileKit.executableFolderURL
/// Executable File
let urlObject = FileKit.executableURL
注意,在 Xcode 中运行和从命令行运行时,executableURL
会有所不同。例如,当运行名为 MySwiftProject
的可执行文件时
file:///Users/username/MySwiftProject/.build/x86_64-apple-macosx10.10/debug/MySwiftProject
file:///Users/username/Library/Developer/Xcode/DerivedData/MySwiftProject-fjgfjmxrlbhzkhfmxdgeipylyeay/Build/Products/Debug/MySwiftProject
。指向包含项目 Package.swift
的目录(通过从包含可执行文件的目录开始向上遍历目录结构来确定),如果未找到 Package.swift
,则指向包含可执行文件的目录。
/// Absolute path to the project's root folder
let stringUrl = FileKit.projectFolder
/// URL to the project's root folder
let urlObject = FileKit.projectFolderURL
提供标准化的工作目录,同时考虑环境变化。在 Xcode 中运行时,它返回包含项目 Package.swift
的目录;在 Xcode 之外运行时,它返回当前工作目录。
/// Absolute path to the present working directory
let stringUrl = FileKit.workingDirectory
/// URL to the project's root folder
let urlObject = FileKit.workingDirectoryURL
注意:由于这是原生 Swift 功能,因此无需导入 FileKit 即可使用。
/// URL pointing to the current source file when it was compiled.
let stringUrl = URL(fileURLWithPath: #file)
有关更多信息,请访问我们的 API 参考。
我们热爱讨论服务器端 Swift 和 Kitura。加入我们的 Slack 与团队交流!
此库在 Apache 2.0 许可下获得许可。完整许可证文本可在 LICENSE 中找到。