Why can't Swift's greater-than or less-than operators compare optionals when the equality operators can?

前端 未结 3 867
温柔的废话
温柔的废话 2020-11-30 15:01

In Swift 3, this is a compile error, if I use > or <

let a: Int?
guard a > 0 else {return}
guard a < 0 else {return}
         


        
3条回答
  •  温柔的废话
    2020-11-30 15:55

    This is because Int and Int? are 2 different things.

    According to the documentation, Int has overloads for < and > and some other operators, while Optional only has overloads for == and !=, see the documentation on Optional, the section talking about Comparing Optional Values.

提交回复
热议问题