zoukankan      html  css  js  c++  java
  • SWIFT 通过字符串创建相关的类

    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.
        }
    }
  • 相关阅读:
    颜色空间RGB与HSV(HSL)的转换
    表示数值的字符串
    正则表达式匹配
    构建乘积数组
    Linux以百万兆字节显示内存大小
    OCP-1Z0-051-题目解析-第26题
    2014华为机试(一)
    android Manifest.xml选项
    TXT小说朗读正式版
    Codeforces Round #256 (Div. 2) B. Suffix Structures
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5218750.html
Copyright © 2011-2022 走看看