Why was UITextField's text property changed to an optional in Swift 2?

后端 未结 4 625
走了就别回头了
走了就别回头了 2020-12-10 00:14

According to the UIKit diff document, in ios9/Swift 2

var text: String! has become var text: String?

According to the documentation

4条回答
  •  不知归路
    2020-12-10 01:11

    A simple way around this problem is to create an extension to UITextField and use that instead of the .text property.

    extension UITextField {
        var unwrappedText: String {
            return self.text ?? ""
         }
    }
    

    Now you can say textfield.unwrappedText without worrying about optionals. (Of course this is just for reading the value).

提交回复
热议问题