[英]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.