3
我已将几个JSON文件添加到我的macOS命令行工具项目中,但似乎找不到它们通常的方式。在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中讨论,直到我弄明白为止。
谢谢!有没有什么好的方法来获取相对于当前.xcodeproj文件的文件路径? (试图将文件包含在git仓库中) – XmasRights
http://stackoverflow.com/questions/31480186/get-path-to-swift-script-from-within-script –