简体   繁体   English

无法将类型“ Class”的值分配给类型“ [Class]”

[英]Cannot assign value of type 'Class' to type '[Class]'

I'm setting up my first Swift project, and I'm currently getting this error: 我正在设置我的第一个Swift项目,并且当前出现此错误:

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

I have a sneaking suspicion this is related to all this ? 我暗中怀疑这与这一切有关? ! business... 商业...

I thought I had the ? 我以为我有? s figured out and enough of a working understanding of the ! 找出并充分理解了! s to get started, but when I started looking into app delegate initialization I got confused by the way they're using the ! 入门,但是当我开始研究应用程序委托初始化时,我对他们使用!的方式感到困惑! s. 秒。 This is what I've got so far in my didFinishLaunchingWithOptions method: 到目前为止,这是我在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();

Line 4 (with the ****) is the one with the problem. 第4行(带****)是有问题的那一行。 Initially I was using self.rootVC as UIViewController without the ! 最初,我使用self.rootVC as UIViewController而不使用! , but I got an error that asked me to add the ! ,但出现错误,要求我添加! , so I did. , 所以我做了。 Now I get the error I'm having now. 现在,我得到了我现在遇到的错误。 This is what my class properties look like: 这是我的类属性的样子:

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

var rootVC: RootViewController?;

The UINavigationController instantiation confuses me as well, to be honest... The reason we use the ? 老实说,UINavigationController实例化也使我感到困惑...我们使用?的原因? is because we want the variable to be able to contain nil, and to not have to define it before we create the object, right? 是因为我们希望变量能够包含nil,而不必在创建对象之前对其进行定义,对吗? So why the () at the end? 那么,为什么在()结尾呢? But I digress. 但是我离题了。

What is the difference between 'UIViewController' and '[UIViewController]' , and what can I do to make this error go away? 'UIViewController''[UIViewController]'之间有什么区别,我该怎么办才能使此错误消失?

Try 尝试

self.navCtrlr!.viewControllers = [self.rootVC! as UIViewController];

[UIViewController] is Array in swift [UIViewController]是快速数组

Besides,I think the rootViewController is the navigationController,so 此外,我认为rootViewController是navigationController,所以

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法分配类型“ Class”的值 <T> &#39;键入&#39;协议 <UIImagePickerControllerDelegate, UINavigationControllerDelegate> &#39; - Cannot assign value of type 'Class<T>' to type 'protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?' 无法将类实例分配给其协议类型? - Cannot assign class instance to its protocol type? 为什么我收到错误“无法将类型(类)的值分配给类型 UICollectionViewDelegate、UICollectionViewDataSource?” - Why I get error “cannot assign value of type (class) to type UICollectionViewDelegate, UICollectionViewDataSource?” 无法分配值类型 - Cannot not assign Value Type Canot将类型“ UIView”的值分配给类型-符合UIView的类- - Canot assign value of type 'UIView' to type --class conforming to UIView-- 无法分配通用类型的值 - Cannot assign value of generic type 无法将String类型的值分配为UITextField类型 - Cannot assign a value of type String to type UITextField 无法将NSMutableArray类型的值分配给UIBarButtonItem类型 - Cannot assign value of type NSMutableArray to type UIBarButtonItem 无法将类型“布尔”的值分配给类型“ UIButton?” - Cannot assign value of type 'Bool' to type 'UIButton?' 无法分配“ NSDictionary”类型的值? 键入“字符串?” - Cannot assign value of type 'NSDictionary?' to type 'String?'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM