Getting path for resource in Command Line Tool

2019-02-17 18:03发布

问题:

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:

  • 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")
    }
    

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.

EDIT: 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!