Preface
I'm bored recently and I've read a local json, but I didn't expect to use Swift to test the data and found that I kept loading the local json file:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
It turns out that there is a comment in front of the local json file/* chrome-extension://pkgccpejnmalmdinmhkkfafefagiiiad/template/fehelper_jsonformat.html */
Then I can't read the "`` pit, remove the comments and read them normally
let path = (forResource: "countryData", ofType: "json") let url = URL(fileURLWithPath: path!) // Methods with throws need to throw exceptions do { /* * The difference between try and try! * An exception occurs and will jump to the catch code * try! The program will crash directly if an exception occurs */ let data = try Data(contentsOf: url) let jsonData:Any = try (with: data, options: ) let jsonArr = jsonData as! NSArray for dict in jsonArr { print(dict) } } catch let error as Error! { print("There was an error reading local data!",error) }
Extended reading--Apple official website Error Handling
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.