【问题标题】:Cannot assign value of type 'Class' to type '[Class]'无法将类型“类”的值分配给类型“[类]”
【发布时间】:2015-11-20 03:23:29
【问题描述】:

我正在设置我的第一个 Swift 项目,我目前遇到了这个错误:

Cannot assign value of type 'UIViewController' to type '[UIViewController]'

我偷偷怀疑这与? ! 业务有关...

我以为我已经弄清楚了 ?s 并且对 !s 有足够的了解以开始使用,但是当我开始研究应用程序委托初始化时,我对他们使用的方式感到困惑!s。这是我到目前为止在我的didFinishLaunchingWithOptions 方法中得到的:

    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 as UIViewController 没有!,但我收到一个错误,要求我添加!,所以我这样做了。现在我得到了我现在遇到的错误。这是我的类属性的样子:

var window: UIWindow?;
var navCtrlr = UINavigationController?();

var rootVC: RootViewController?;

UINavigationController 实例化也让我感到困惑,老实说......我们使用 ? 的原因是因为我们希望变量能够包含 nil,并且不必在创建对象之前定义它, 对?那么为什么最后是()?但我离题了。

'UIViewController''[UIViewController]' 之间有什么区别,我该怎么做才能消除此错误?

【问题讨论】:

    标签: ios swift uiviewcontroller uinavigationcontroller


    【解决方案1】:

    试试

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 2019-12-03
      • 2017-08-07
      • 2021-03-08
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多