通过字符串来创建控制器
- 如果通过字符串来创建控制器
- 不可以直接通过类型来获取对应的类
- 因为Swift有命名空间,类前需要加上命名空间的名称
- 获取命名空间的名称
let executable = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String
guard let childVcClass : AnyClass = NSClassFromString(executable + "." + childCVcName) else {
XMGLog("转成对应的类失败")
return
}
let childClass = childVcClass as! UITableViewController.Type
let childVc = childClass.init()
override func viewDidLoad() {
super.viewDidLoad()
self.addChildViewController("HomeViewController", imageName: "tabbar_home", title: "主页")
self.addChildViewController("MessageViewController", imageName: "tabbar_message_center", title: "消息")
self.addChildViewController("DiscoverViewController", imageName: "tabbar_discover", title: "广场")
self.addChildViewController("ProfileViewController", imageName: "tabbar_profile", title: "我")
}
private func addChildViewController(childCVcName: String, imageName : String, title : String) {
guard let executable = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else {
XMGLog("没有命名空间")
return
}
guard let childVcClass : AnyClass = NSClassFromString(executable + "." + childCVcName) else {
XMGLog("转成对应的类失败")
return
}
let childClass = childVcClass as! UITableViewController.Type
let childVc = childClass.init()
let homeNav = UINavigationController(rootViewController: childVc)
childVc.title = title
childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
childVc.tabBarItem.image = UIImage(named: imageName)
self.addChildViewController(homeNav)
}
}
读取json文件.创建对应的控制器
- 读取json文件
- 从服务器获取json文件(本地)
- 加载对应的json文件
- 通过序列化转成对应的类型
let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
guard let data = NSData(contentsOfFile: path!) else {
XMGLog("没有获取到json数据")
addChildViewController()
return
}
guard let childVcArray = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [[String : AnyObject]] else {
print("没有获取到值")
addChildViewController()
return
}
- 注意:
JSONObjectWithData(data: NSData, options opt: NSJSONReadingOptions)
函数会抛出异常
- 如果发现在调用一个方法时,会出现throws.则需要处理异常
- 处理异常三种方式
处理异常三种方式
1.try方式
do {
let childVcArray = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
} catch {
XMGLog(error)
}
2.try?方式
如果有值,则返回对应的值.如果没有值,则返回nil.
也就是:返回的类型时一个可选类型
3.try!方式:不建议使用
相当于告诉系统一定没有异常,该方式非常危险.很容易崩溃
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
guard let data = NSData(contentsOfFile: path!) else {
XMGLog("没有获取到json数据")
addChildViewController()
return
}
guard let childVcArray = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [[String : AnyObject]] else {
print("没有获取到值")
addChildViewController()
return
}
for dict in childVcArray {
let vcName = dict["vcName"] as! String
let title = dict["title"] as! String
let imageName = dict["imageName"] as! String
addChildViewController(vcName, imageName: imageName, title: title)
}
}
private func addChildViewController() {
self.addChildViewController("HomeViewController", imageName: "tabbar_home", title: "主页")
self.addChildViewController("MessageViewController", imageName: "tabbar_message_center", title: "消息")
self.addChildViewController("DiscoverViewController", imageName: "tabbar_discover", title: "广场")
self.addChildViewController("ProfileViewController", imageName: "tabbar_profile", title: "我")
}
private func addChildViewController(childCVcName: String, imageName : String, title : String) {
guard let executable = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else {
XMGLog("没有命名空间")
return
}
guard let childVcClass : AnyClass = NSClassFromString(executable + "." + childCVcName) else {
XMGLog("转成对应的类失败")
return
}
let childClass = childVcClass as! UITableViewController.Type
let childVc = childClass.init()
let homeNav = UINavigationController(rootViewController: childVc)
childVc.title = title
childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
childVc.tabBarItem.image = UIImage(named: imageName)
self.addChildViewController(homeNav)
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步