代码之家  ›  专栏  ›  技术社区  ›  Binayak Pandey

处理以Swift 2编写的付款代码,不确定是否等同于Swift 3

  •  0
  • Binayak Pandey  · 技术社区  · 7 年前

    if self.expireDateTextField.text.isEmpty == false {
        let expirationDate = self.expireDateTextField.text.componentsSeparatedByString("/")
        let expMonth = UInt(expirationDate[0].toInt()!)
        let expYear = UInt(expirationDate[1].toInt()!)
    

    我知道 toInt() 已删除,但与上述代码等效的是什么?

    1 回复  |  直到 7 年前
        1
  •  0
  •   glyvox    7 年前

    Swift 3相当于 toInt() 只是 Int()

    let expMonth = UInt(Int(expirationDate[0])!)!
    let expYear = UInt(Int(expirationDate[1]!))!
    

    在您的情况下,我将对代码进行两个改进。首先,您要转换两次,这不是必需的,您可以使用 UInt() 只有

    guard let expMonth = UInt(expirationDate[0]),
        let expYear = UInt(expirationDate[1]) else {
        print("Error in expiration casting")
    
        return
    }