【问题标题】:Enum case switch not found in type在类型中找不到枚举大小写开关
【发布时间】:2016-02-22 06:16:20
【问题描述】:

有人可以帮我解决这个问题吗?

我有以下public enum

public enum OfferViewRow {
    case Candidates
    case Expiration
    case Description
    case Timing
    case Money
    case Payment

}

还有以下 mutableProperty:

private let rows = MutableProperty<[OfferViewRow]>([OfferViewRow]())

在我的初始化文件中,我使用一些 reactiveCocoa 来设置我的 MutableProperty:

rows <~ application.producer
    .map { response in
        if response?.application.status == .Applied {
            return [.Candidates, .Description, .Timing, .Money, .Payment]
        } else {
            return [.Candidates, .Expiration, .Description, .Timing, .Money, .Payment]
        }
}

但是现在的问题是,当我尝试在行中获取枚举的值时,它会引发错误。请看下面的代码。

 func cellViewModelForRowAtIndexPath(indexPath: NSIndexPath) -> ViewModel {
        guard
            let row = rows.value[indexPath.row],
            let response = self.application.value
            else {
                fatalError("")
        }

        switch row {
        case .Candidates:
             // Do something
        case .Expiration:
            // Do something
        case .Description:
           // Do something
        case .Timing:
           // Do something
        case .Money:
           // Do something
        case .Payment:
           // Do something
        }
    }

它会抛出一个错误:Enum case 'some' not found in type 'OfferViewRow 就行了let row = rows.value[indexPath.row]

在它抛出的每个 switch 语句上:Enum case 'Candidates' not found in type '&lt;&lt;Error type&gt;&gt;

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你可以试试public enum OfferViewRow: Int 作为枚举声明
  • 没救了,也试过了!
  • 常量row的类型是什么?

标签: ios swift enums swift2 reactive-cocoa


【解决方案1】:

guard 语句需要一个可选的,如错误消息中的“Enum case 'some'”所暗示的那样。

但是rows.value[indexPath.row] 不是Optional&lt;OfferViewRow&gt;,它是一个原始的OfferViewRow。所以它不会进入守卫语句。

let row = rows.value[indexPath.row] 上移一行:Swift 负责边界检查,如果 indexPath.row 超出边界,则会崩溃。

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2023-01-18
    • 2011-06-20
    • 1970-01-01
    • 2015-10-27
    相关资源
    最近更新 更多