I'm trying to load some JSON data from a local file.
In this Apple doc it says:
Manage the data files for your app using the asset catalog. A file can contain any sort of data except device executable code generated by Xcode. You can use them for JSON files, scripts, or custom data types
So I added a new data set and dropped the JSON file inside. Now I can see it under Assets.xcassets folder (Colours.dataset folder with colours.json and Contents.json inside it)
I found this SO answer that shows how to read a JSON file and I'm using this code to read the file:
if let filePath = NSBundle.mainBundle().pathForResource("Assets/Colours", ofType: "json"), data = NSData(contentsOfFile: filePath) {
print (filePath)
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
print(json)
}
catch {
}
} else {
print("Invalid file path")
}
But this code is printing "Invalid file path" and not reading the file. I also tried "Colours" and "Colours.json" but to no avail.
Could anyone please tell me how to properly add a local JSON file and read it?
Thanks.