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.
In the Terminal app on your Mac, invoke a command-line editor by typing the name of the editor, followed by a space and then the name of the file you want to open. If you want to create a new file, type the editor name, followed by a space and the pathname of the file.
In the Terminal app on your Mac, press the Up Arrow key. The last command you entered appears on the command line. Continue pressing the Up Arrow key until you see the command you want, then press Return.
The Mac command line is a program called Terminal. It lives in the /Applications/Utilities/ folder. To find it, go to your Applications folder. Near the bottom, there is a folder called Utilities.
You can use the command-line environment interactively by typing a command and waiting for a result, or you can use the shell to compose scripts that run without direct interaction. In the Terminal app on your Mac, enter the complete pathname of the tool’s executable file, followed by any needed arguments, then press Return.
For example, to run MyCommandLineProg, use the following: To open an app, use the open command: When entering commands, if you get the message command not found, check your spelling. Here’s an example: In the Terminal app on your Mac, click the Terminal window that is running the command you want to terminate.
When building an app for the Mac, you can embed a command-line tool in the app to act as a helper tool. Instances where this may be helpful include, for example: You want to run some code in a separate process. In many cases, an XPC service is a better choice for this, but sometimes it’s easier to embed a command-line tool.
To run a command in the current user’s home folder, precede it with the folder specifier. For example, to run MyCommandLineProg, use the following: To open an app, use the open command:
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:
JSONMocks
).Add files to this bundle. Add the file to Xcode, then make sure that the bundle is ticked in its target membership section:
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:
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")!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With