SWIFT 通过字符串创建相关的类
2016-02-25 23:11
351 查看
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) // Override point for customization after application launch. self.window!.backgroundColor = UIColor.whiteColor() self.window?.rootViewController = RootViewController() self.window!.makeKeyAndVisible() return true } }
import UIKit class RootViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let name = "MainViewController" //动态获取命名空间 let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String print(namespace) //注意工程中必须有相关的类,否则程序会崩 let cls:AnyObject = NSClassFromString(namespace + "." + name)! print(cls) // 告诉编译器它的真实类型 let viewControllerClass = cls as! UIViewController.Type let viewController = viewControllerClass.init() print(viewController) } }
import UIKit class MainViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
相关文章推荐
- 5.Swift枚举|结构体|类|属性|方法|下标脚本|继承
- swift基本语法(总结提炼版)之010 swift 之函数
- Project Perfect让Swift在服务器端跑起来-Perfect in Visual Studio Code (四)
- IOS Apps 开发(Swift)(5)——Work with View Controllers
- Swift入门学习(1):变量
- swift 枚举
- swift 自定义控件三部曲
- The Swift Programming Language学习笔记(二十三)——协议
- The Swift Programming Language学习笔记(二十二)——扩展
- The Swift Programming Language学习笔记(二十一)——嵌套类型
- IOS Apps 开发(Swift)(4)——Connect the UI to Code(2)
- swift 闭包
- 从OC到Swift
- Swift闭包详解
- IOS Apps 开发(Swift)(4)——Connect the UI to Code(1)
- 源码推荐(02.25B):贝塞尔曲线画图,Swift版Tabletableviewcell上加载视频播放器
- Swift变量和常量
- swift 函数
- swift集合类型——数组和字典
- Swift iOS tableView static cell动态计算高度