1 Import UIKit2 3 classMaintabbarviewcontroller:uitabbarcontroller {4 5 Overridefunc viewdidload () {6 super.viewdidload ()7 //1. Get the address of the JSON file8Guard Let JsonPath = Nsbundle.mainbundle (). Pathforresource ("Mainvcsettings.json", Oftype:nil)Else {9 returnTen } one //2. Get the data for the JSON file aGuard Let Jsondata = NSData (contentsoffile:jsonpath)Else { - return - } the //3. Convert JSON file data into Swift readable data -Guard Let Anyobject =Try? Nsjsonserialization.jsonobjectwithdata (jsondata, Options:. Mutablecontainers)Else { - return - } + -Guard Let Dicarray = Anyobject as? [[string:anyobject]]Else { + return a } at //4. Extracting JSON file data - forDicinchDicarray { - //4.1 Extracting Data -Guard Let Vcname = dic["Vcname"] as? StringElse { - Continue - } inGuard Let title = dic["title"] as? StringElse { - Continue to } +Guard Let ImageName = dic["ImageName"] as? StringElse { - Continue the } * //4.2 Adding a controller $ addchildviewcontroller (vcname, title:title, imagename:imagename)Panax Notoginseng } -}
Processing of JSON data in Swift