为什么在此Swift开关中需要默认情况?

时间:2019-02-25 03:40:02

标签: 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
        }
    }
}

0 个答案:

没有答案
相关问题