我相信下面的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
}
}
}