我使用下面的代码来设置根视图控制器。但它给了我错误:
let storyboard = UIStoryboard(name:"SignUp", bundle:nil)
let viewController = storyboard.instantiateInitialViewController() as! UIViewController
self.window!.rootViewController = viewController
我收到的错误如Downcast from 'UIViewController?' to 'UIViewController' only unwraps optionals; did you mean to use '!'?
我认为,它与" as!
"有关,但它是在新的swift中引入的。
有人有想法吗?
答案 0 :(得分:2)
我认为有两种方法可以做到这一点
更安全一个:
let storyboard = UIStoryboard(name:"SignUp", bundle:nil)
if let viewController = storyboard.instantiateInitialViewController(){
self.window!.rootViewController = viewController
}
修改:已移除as? UIViewController
编译器建议的那个:
let storyboard = UIStoryboard(name:"SignUp", bundle:nil)
let viewController = storyboard.instantiateInitialViewController()!
self.window!.rootViewController = viewController
基本上编译器只是让你知道这段代码中的as! UIViewController
与使用!