Swift comparing Strings optionals vs non-optional

后端 未结 2 1975
旧时难觅i
旧时难觅i 2021-01-13 10:41

When comparing strings in Swift, you can compare non-optional strings with optional strings.

Like so (text is an optional, and it is empty):

UITextF         


        
2条回答
  •  半阙折子戏
    2021-01-13 10:42

    For every Equatable type the == operation is also defined for optionals:

    public func ==(lhs: T?, rhs: T?) -> Bool
    

    The non-optional on the right side gets automatically promoted to an optional.

    The == for optionals returns true when both values are nil or if they are both non-nil and they are equal.

提交回复
热议问题