首页 > 解决方案 > 为什么我需要在这个 Swift 开关中使用默认情况?

问题描述

我相信以下 Swift 代码应该可以编译,但是编译器会出现Switch must be exhaustive错误。是否有关于Int减法的特殊属性允许“大于 0”、“小于 0”和“等于 0”之外的可能结果?

public enum MediaOrientation {
    case portrait
    case landscape
    case square

    init(width: Int, height: Int) {
        switch width - height {
        case let x where x == 0: self = .square
        case let x where x < 0: self = .portrait
        case let x where x > 0: self = .landscape
        }
    }
}

标签: swift

解决方案


推荐阅读