To get started with Swift, the previous OC was a weak language, and Swift was a strong language, with a big difference in syntax.
//JSON file[ { "name":"Json1", "title":"Test 1", }, { "name":"Json2", "title":"Test 2", }, { "name":"Json3", "title":"Test 3", } ]
/** Loading local files*/ //1. Get the file pathLet path = Nsbundle.mainbundle (). Pathforresource ("Plist.json", Oftype:nil)//2. Create a nsdata from a file path ifLet JsonPath =path {let Jsondata=NSData (Contentsoffile:jsonpath)//a method with throws needs to throw an exception Do { /** There is a possibility that the exception code is placed in this*/ //3. Serialization of data---array /** The difference between try and try! * Try exception will jump to catch code * try! Exception program will be directly crash */Let Dictarr=TryNsjsonserialization.jsonobjectwithdata (jsondata!, Options:NSJSONReadingOptions.MutableContainers)//4. Iterating through an array//To iterate through an array in swift, you must make it clear that the type of the data [[string:string]] means that the key value in the dictionary is a string [[String]] indicating that the array is all strings forDictinchDictarr as![[string:string]] {print (dict)}}Catch { //The exception code is placed hereprint (Error)}}
swift--load local resource file and traverse