在命令行工具中获取资源的路径 [英] Getting path for resource in Command Line Tool
问题描述
我正在尝试在Xcode(8 beta 2)的命令行工具中获取资源的路径。这是我得到的:
I'm trying to get the path of a resource in a Command Line Tool in Xcode (8 beta 2). Here's what I've got:
- 资源file.xyz已拖到项目中,并且目标成员资格与主项目匹配
- 在构建阶段->复制文件下,目标设置为资源,子路径为空。取消选中仅在安装时复制,并且下表中列出了file.xyz。
-
在我的main.swift文件中,我有以下代码:
- The resource, file.xyz, has been dragged into the project and the target membership matches the main project.
- Under Build Phases -> Copy Files, the destination is set to "Resources" and the subpath is empty. "Copy only when installing" is unchecked and file.xyz is listed in the table below.
In my main.swift file, I have the following code:
guard let filePath = Bundle.pathForResource("file",
ofType: "xyz",
inDirectory: "Resources") else{
fatalError("Could not find file")
}
每次都触发致命错误
。有任何想法吗?我知道命令行工具没有应用程序捆绑包,这就是为什么将资源复制到Resources文件夹中的原因,但是我的 inDirectory
参数有点疯狂猜猜……谢谢您的阅读。
The fatalError
is triggered every time. Any ideas? I understand that Command Line Tools don't have an application bundle and that's why the resource is being copied into the Resources folder instead, but my inDirectory
argument is a bit of a wild guess… Thanks for reading.
编辑:我的主要目标是从CLT访问资源文件(在这种情况下为文本文件),但我并未附加这种特定的操作方式,因此,如果还有另一种方法也很棒!
My primary goal is to access a resource file (text file, in this case) from a CLT—I'm not attached to this specific way of doing it, so if there's an alternate approach that's great too!
推荐答案
作为解决方法,您可以使用源文件
的绝对路径例如,如果在iCloud驱动器上:
As a workaround, you can use the absolute path of the source file For example if on iCloud drive :
let path = / Users / myName / Library /移动版
文档/com~apple~CloudDocs/myDirectory/file.xyz
let path = "/Users/myName/Library/Mobile Documents/com~apple~CloudDocs/myDirectory/file.xyz"
这篇关于在命令行工具中获取资源的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!