Search code examples
swiftxcodecommand-line-tool

How to copy bundle resources for command line tool project in Xcode?


I've been trying to access a file in my command line tool Xcode project but program isn't able to find the file path. I'm assuming it's because the file isn't being copied to my project.

I've gone to build phases and added my file (graph2.net) but the program can't find the file.

enter image description here

func readGraph() {
    if let path = Bundle.main.path(forResource: "graph2", ofType: "net") {
        print(path)
    } else {
        print("Failed")
    }
}

always returns failed. However in an iOS app, the option to copy bundle resources exists and I am able to find the file. How can I solve this issue?


Solution

  • A command-line-tool only consists of an executable. No bundle is created and what you are trying to do is not possible.

    You’ll have to place the file in some other place and load it from there.