简体   繁体   English

使用多个ViewController初始化类问题(Swift)

[英]Initializing class issue using multiple ViewControllers (Swift)

I'm working with two view controllers in my XCode project. 我正在XCode项目中使用两个视图控制器。 I passed a variable from the first View Controller to the second. 我将变量从第一个View Controller传递给第二个。 And I keep getting this Swift Complier Error: "Class SecondViewController has no initializers" Fix-it > Stored property 'XScore' without initial value prevents synthesised initializers . 而且,我不断收到此Swift Complier错误:“类SecondViewController没有初始化程序” Fix-it>存储的属性'XScore'没有初始值会阻止合成初始化程序

This is how I passed the variable in the FirstViewController: 这是我在FirstViewController中传递变量的方式:

override func prepareForSegue(Segue: UIStoryboardSegue, sender: AnyObject?) {
    var Destination = (Segue.destinationViewController as SecondViewController)

    Destination.XScore = Score

    }

This is (part of) my Second View Controller: 这是我的第二个视图控制器的(一部分):

class SecondViewController: UIViewController {

@IBOutlet weak var XScoreLabel: UILabel!

var XScore:Double = 0.00

I have tried many different ways to initialize this property even when it seems right to me without. 我尝试了许多不同的方法来初始化此属性,即使在我看来没有它也是如此。

Thanks in advance. 提前致谢。

You have a couple of problems. 你有几个问题。 As Gabe points out, you are casting your destination as a type SecondViewController , but are showing code for a different class, ShopViewController . 正如Gabe指出的那样,您将目标类型SecondViewControllerSecondViewController ,但显示的是另一个类ShopViewController代码。

You need to show us the declaration of XScore in SecondViewController . 您需要向我们展示XScoreSecondViewController的声明。

If it's defined (in SecondViewController) as 如果在SecondViewController中将其定义为

var XScore:Double 

then change it to 然后将其更改为

var XScore:Double  = 0

or 要么

var XScore:Double? = nil 

(to allow for nil values) (以允许零值)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM