func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) // 依据String名字拿到控制器(添加项目名称,命名空间,不能有数字和特殊符号) // 返回的是AnyClass? 需要as?强转 // 控制器添加Type类型 let rootControl = NSClassFromString("SwiftyDemo.ViewController") as? UIViewController.Type let vc = rootControl?.init() window?.rootViewController = vc window?.makeKeyAndVisible() return true }
同时可以用info.plist获取命名空间
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) // 依据String名字拿到控制器(添加项目名称,命名空间,不能有数字和特殊符号) // 返回的是AnyClass? 需要as?强转 // 控制器添加Type类型 // let rootControl = NSClassFromString("SwiftyDemo.ViewController") as? UIViewController.Type // 获取命名空间的值,可选 let str = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "" let con = NSClassFromString(str + "." + "ViewController") as? UIViewController.Type let vc = con?.init() window?.rootViewController = vc window?.makeKeyAndVisible() return true }
时间: 2024-10-05 04:58:25