【问题标题】:ViewController has no initialisers?ViewController 没有初始化程序?
【发布时间】:2016-06-21 06:24:36
【问题描述】:

我正在尝试从模型类访问枚举以编写 switch case 来执行 segue。这是我的代码:

class LandingViewController: UIViewController {

// MARK: Private Structs.

private struct SegueIdentifier {
    static let forcedUpdate = "forcedUpdate"
    static let optionalUpdate = "optionalUpdate"
}


// MARK: Private variables.

private let updateType: StartupManager.UpdateType

// MARK: LifeCycle Methods.

override func viewDidLoad() {
    super.viewDidLoad()
    StartupManager.setupForLanding()

    var segueIdentifier: String
    switch updateType {
    case .ForcedUpdate: segueIdentifier = SegueIdentifier.forcedUpdate
    case .OptionalUpdate: segueIdentifier = SegueIdentifier.optionalUpdate
    }
    performSegueWithIdentifier(segueIdentifier, sender: nil)
}

setupForLanding() 用于检查 startUpManager 模型类以查看哪个枚举被触发。

class StartupManager: NSObject {

enum UpdateType {
    case OptionalUpdate
    case ForcedUpdate
}
// code to perform a check
if isForceUpdate {
   completion(.ForcedUpdate)
} else {
   completion(.OptionalUpdate)
}

但我不断收到错误消息LandingViewController has no initialisers。如何在启动管理器中检查调用了哪个案例,然后在landingViewController中执行segue?

【问题讨论】:

  • 正如 Thien Liu 指出的,问题在于 updateType 没有默认类型。要么将其默认为某个值(或使其成为可选值)。但是,顺便说一句,您可能不想在viewDidLoad 中执行转场。您可能希望将其推迟到流程的后期,例如viewDidAppear。根据您转换到此场景的方式,您可能会收到有关在另一个转换中间启动转换的错误。

标签: ios swift enums swift2 segue


【解决方案1】:

您必须为 updateType

初始化值

private let updateType: StartupManager.UpdateType = .OptionalUpdate 例如

【讨论】:

    【解决方案2】:

    您的问题在这里:private let updateType: StartupManager.UpdateType 。在 swift 中,所有值都应该在你使用它们之前被初始化。为此,您应该在构造函数 (init) 中对其进行初始化或分配值,例如:private let updateType: StartupManager.UpdateType = value,或使用可选值。

    最好的问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      相关资源
      最近更新 更多