【发布时间】:2017-10-05 05:30:24
【问题描述】:
我已将一些 JSON 文件添加到我的 macOS 命令行工具项目中,但我似乎无法以通常的方式找到它们。
if let path = Bundle.main.path(forResource: "Data", ofType: "json")
{
if let contents = try? String(contentsOfFile: path)
{
print (contents)
}
else { print("Could not load contents of file") }
}
else { print("Could not find path") }
此代码在基于视图的应用程序中使用时绝对正常,但总是在命令行工具中打印“找不到路径”。
有谁知道我做错了什么?
P.S:完全知道我应该为此使用 guard 语句,但代码不在函数中,只是在 main.swift 中胡闹,直到我弄明白为止。
【问题讨论】:
标签: swift xcode bundle command-line-tool