J son转字典 do catch try try!


//
//  DSTabBarController.swift
//  DSWeibo
//
//  Created by 刘小二 on 16/5/22.
//  Copyright © 2016年 刘小二. All rights reserved.
//

import UIKit

class DSTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        addchildViewControllers()
        
    }
    
    
   private func addchildViewControllers()
    {
        
        // 设置全局的文字图片的选中颜色
        tabBar.tintColor = UIColor.orangeColor()
        
        // 添加子控制器
//        let home = HomeViewController()
//        addChildViewController(childController:home, title: "首页", image: "tabbar_home", selectedImage: "tabbar_home" + "highlighted")
//        let discover = DicoverViewController()
//        addChildViewController(childController:discover, title: "发现", image: "tabbar_discover", selectedImage: "tabbar_discover" + "highlighted")
//        let message = MessageViewController()
//        addChildViewController(childController:message, title: "消息", image: "tabbar_message_center", selectedImage: "tabbar_message_center" + "highlighted")
//        let mine = MineViewController()
//        addChildViewController(childController: mine, title: "我的", image: "tabbar_profile", selectedImage: "tabbar_profile" + "highlighted")
        
        // 获取json数据
        
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        // 可选类型判断
        if let jsonPath = path {
        
            let data = NSData(contentsOfFile: jsonPath)
            
            if let jsonData = data {
            
                // 捕捉异常 throws
                
                do {
                    
                let arr = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)
                    //  数组必须有确定的类型
                    for dict in arr as! [[String:String]]
                    {
                        // 函数的参数类型不是可选的不能为nil必须 !
                        addChildViewController(childController: dict["vcName"]!, title: dict["title"]!, image: dict["imageName"]!, selectedImage: dict["imageName"]! + "highlighted")
                    
                    }
                    
                    
                }catch {
                
                // 错误处理
                    print(error)
                            // 使用字符串创建对应的类
                    addChildViewController(childController: "HomeViewController", title: "首页", image: "tabbar_home", selectedImage: "tabbar_home" + "highlighted")
                    addChildViewController(childController: "DicoverViewController", title: "发现", image: "tabbar_discover", selectedImage: "tabbar_discover" + "highlighted" )
                    addChildViewController(childController: "MessageViewController", title: "消息", image: "tabbar_message_center", selectedImage: "tabbar_message_center" + "highlighted" )
                    addChildViewController(childController: "MineViewController", title: "我的", image: "tabbar_profile", selectedImage: "tabbar_profile" + "highlighted" )

                }
                
            }
        }
        

    }

    
}


extension DSTabBarController
{
    // MARK: - 添加子控制器的方法
    func addChildViewController(childController childController: String , title: String , image : String , selectedImage : String) {
        
        // 得到命名空间
        
        let mm = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        
        
        let vcc:AnyClass? = NSClassFromString(mm + "." + childController)
        let vc = vcc as! UIViewController.Type
        
        let oc = vc.init()
        
        oc.tabBarItem.title = title
        oc.tabBarItem.image = UIImage(named:image)
        oc.tabBarItem.selectedImage = UIImage(named: selectedImage)
        let nav = DSNavigationController(rootViewController:oc)
        addChildViewController(nav)
    }

    
}

你可能感兴趣的:(J son转字典 do catch try try!)