Your problem is that you are forcibly unwrapping the values, and in case of an error you cannot know where it came from.
Instead, you should handle errors and safely deploy your options.
And as @vadian correctly points out in his comment, you should use Bundle.main.url .
private func readJson() { do { if let file = Bundle.main.url(forResource: "points", withExtension: "json") { let data = try Data(contentsOf: file) let json = try JSONSerialization.jsonObject(with: data, options: []) if let object = json as? [String: Any] { // json is a dictionary print(object) } else if let object = json as? [Any] { // json is an array print(object) } else { print("JSON is invalid") } } else { print("no file") } } catch { print(error.localizedDescription) } }
When coding in Swift usually ! is the smell of code. Of course, there are exceptions (IBOutlets and others), but try not to use a power scan with ! independently and always deploy safely.
ayaio Nov 05 '16 at 13:34 2016-11-05 13:34
source share