您的位置:首页 > 移动开发 > Swift

【Swift】通过类名创建对象

2017-09-08 15:19 162 查看
import Foundation
import UIKit

extension NSObject {

func swiftClassFromString(className: String) -> UIViewController! {
// get the project name
if  let appName: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as! String? {
//拼接控制器名
let classStringName = "\(appName).\(className)"
//将控制名转换为类
let classType = NSClassFromString(classStringName) as? UIViewController.Type
if let type = classType {
let newVC = type.init()
return newVC
}
}
return nil;
}
}

//将控制器名转换为类
let vc = self.swiftClassFromString(className)
self.navigationController!.pushViewController(vc, animated: true)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swift
相关文章推荐