swift--load the local resource file and traverse

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.