16

I've added a few JSON files to my macOS Command Line Tool project, but I can't seem to find them the usual way.

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

This code works absolutely fine when used in a View based application, but always prints "Could not find path" in the command line tool.

Does anyone know what I'm doing wrong?

P.S: Fully aware that I should be using guard statements for this, but the code isn't in a function, just faffing about in main.swift till I figure this out.

XmasRights
  • 1,427
  • 13
  • 21

2 Answers2

23

You can create a separate bundle, place the files there, and then load them without having to worry about their individual URLs.

Let's do it:

  1. In Xcode, click on File --> New --> Target...
  2. Click on the macOS tab at the top
  3. Scroll down to the Framework & Library, select Bundle, click Next. Give a name to the new bundle (let's say JSONMocks).
  4. Add files to this bundle. Add the file to Xcode, then make sure that the bundle is ticked in its target membership section:

    target membership for file in bundle

  5. Add the bundle to your target. In the target's Build Phases, open the Copy Files phase and click the + button. Select the bundle and click Add:

    enter image description here

  6. Programmatically access the contents of your bundle thus:

let currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let bundleURL = URL(fileURLWithPath: "JSONMocks.bundle", relativeTo: currentDirectoryURL)
let bundle = Bundle(url: bundleURL)
let jsonFileURL = bundle!.url(forResource: jsonFileName, withExtension: "json")!
Eric
  • 16,003
  • 15
  • 87
  • 139
12

There is no app bundle for a command-line tool. Just a binary.

You need to put the JSON file in a known location (current directory?) and construct the path yourself

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • 1
    Thanks! is there any nice way to get the path to a file relative to the current .xcodeproj file? (trying to include the files in a git repo) – XmasRights May 07 '17 at 01:23
  • 2
    http://stackoverflow.com/questions/31480186/get-path-to-swift-script-from-within-script – Lou Franco May 07 '17 at 11:24