swift json文字列の変換と異常throws

1752 ワード


        // 1.  json     
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        // 2.        NSData
        if let jsonPath = path{
            let jsonData = NSData(contentsOfFile: jsonPath)
            
            do{
                //               
                // 3.   json   --> Array
                // try :        catch     
                // try! :           
                let dictArr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
//                print(dictArr)
                // 4.    ,             
                //  Swift ,           ,          
                for dict in dictArr as! [[String: String]]
                {
                    //         addChildViewController      ,              
                    addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
                }
                
            }catch
            {
                //             
                print(error)
                
                //         
                addChildViewController("HomeTableViewController", title: "  ", imageName: "tabbar_home")
                addChildViewController("MessageTableViewController", title: "  ", imageName: "tabbar_message_center")
                addChildViewController("DiscoverTableViewController", title: "  ", imageName: "tabbar_discover")
                addChildViewController("ProfileTableViewController", title: " ", imageName: "tabbar_profile")
            }
        }