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)
    }

    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 完整的do-try-catch错误处理模式的语法如下: do { try 语句 成功处理语句组 } catch...
    星空梦想阅读 7,727评论 1 5
  • Objective-C使用@try @catch @finally来捕获并处理异常。处理异常需要用到NSExcep...
    rogerwu1228阅读 1,186评论 0 0
  • 戒 觥筹交错中 热血涌上心口 我看到的是你 前世的约定 定 一朵花开的声音 只有欣赏的人懂 一片叶落的苍凉 自有怜...
    槿柔伊阅读 218评论 0 1
  • 弦樂 小提琴 音色大小和音域不同,小提琴,中提琴,大提琴和低音大提琴組成交響樂團的弦樂部分。 尺寸最小,音域最高,...
    咔辣辣阅读 77评论 0 0