I have added some JSON files to my macOS command line project, but I cannot find them in 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 "Cannot find the path" in the command line tool.
Does anyone know what I'm doing wrong?
PS: Fully aware that for this I must use instructions for this guard, but the code is not in the function, just cheating in main.swift until I figure it out.
source
share