Ios 无法分配类型为';类别';输入';[类别]和#x27;
我正在设置我的第一个Swift项目,目前我遇到以下错误:Ios 无法分配类型为';类别';输入';[类别]和#x27;,ios,swift,uiviewcontroller,uinavigationcontroller,Ios,Swift,Uiviewcontroller,Uinavigationcontroller,我正在设置我的第一个Swift项目,目前我遇到以下错误: Cannot assign value of type 'UIViewController' to type '[UIViewController]' 我暗自怀疑这与所有这些?有关业务 我想我已经弄明白了?,并且对有了足够的理解开始,但当我开始研究应用程序委托初始化时,我被他们使用的方式弄糊涂了s。这是我迄今为止在我的didfishlaunchingwithoptions方法中得到的: self.window = UIWindo
Cannot assign value of type 'UIViewController' to type '[UIViewController]'
我暗自怀疑这与所有这些?
有关代码>业务
我想我已经弄明白了?
,并且对有了足够的理解代码>开始,但当我开始研究应用程序委托初始化时,我被他们使用的方式弄糊涂了代码>s。这是我迄今为止在我的didfishlaunchingwithoptions
方法中得到的:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
self.navCtrlr = UINavigationController();
self.rootVC = RootViewController(nibName: nil, bundle: nil);
self.navCtrlr!.viewControllers = self.rootVC! as UIViewController; ****
self.window!.rootViewController = self.rootVC;
self.window?.makeKeyAndVisible();
第4行(带****)就是有问题的那一行。最初我使用的是self.rootVC作为UIViewController
,没有代码>,但我遇到一个错误,要求我添加代码>,所以我做了。现在我得到了我现在的错误。这就是我的类属性的外观:
var window: UIWindow?;
var navCtrlr = UINavigationController?();
var rootVC: RootViewController?;
说实话,UINavigationController实例化也让我感到困惑。。。我们之所以使用?
是因为我们希望变量能够包含nil,并且在创建对象之前不必定义它,对吗?那么,为什么最后会出现()
?但我离题了
在'UIViewController'
和'UIViewController]
之间有什么区别,我可以做些什么来消除这个错误?试试看
self.navCtrlr!.viewControllers = [self.rootVC! as UIViewController];
[UIViewController]
是swift中的数组
此外,我认为rootViewController
是导航控制器,所以
self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
self.rootVC = RootViewController(nibName: "youNibName", bundle: nil);
self.navCtrlr = UINavigationController(rootViewController: self.rootVC!);
self.window!.rootViewController = self.navCtrlr;
self.window?.makeKeyAndVisible();