【问题标题】:Checking if text fields are empty cause error in Swift 2检查文本字段是否为空导致 Swift 2 中的错误
【发布时间】:2015-10-10 08:37:45
【问题描述】:

我正在尝试检查文本框是否没有价值。

当我这样做时:

if(userEmail?.isEmpty || userPassword?.isEmpty || userPasswordRepeat?.isEmpty)  

我收到以下错误

我尝试添加“?”在“.isEmpty”之前,但错误不会消失

有什么想法吗?

【问题讨论】:

  • 它在错误消息中:可选值未解包。使用! 解包一个值并阅读optional values
  • @EICaptain textfield.texttextfield.stringValue 有区别吗?
  • @EICaptain 我在问。我习惯用stringValue
  • @Arc676 stringValue 当您的 iPhone 或 iPad 开发应用程序在 mac 应用程序中可用时,属性不是 textfiled 的成员...
  • 这也是一个包含变量的屏幕截图。 ge.tt/3NeaYbP2/v/0?c

标签: ios swift uitextfield swift2 swift2.1


【解决方案1】:

试试这个……

if txtEmail.text?.isEmpty == true || txtPassword.text?.isEmpty == true || txtRePassword.text?.isEmpty == true{
        print("true")
}

【讨论】:

  • 还是不行。这是我收到的错误消息。 ge.tt/8F7FbbP2/v/0?c
  • 很高兴它有帮助...很高兴帮助你:)
【解决方案2】:

如果也对肯定情况感兴趣,以下是 Swift 2 的替代解决方案:

let email = self.txtEmail.text where !email.isEmpty, let password = self.txtPassword.text where !password.isEmpty {
    //all fields are not nil && not empty
}else{
    //some field is nil or empty
}

【讨论】:

    猜你喜欢
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2015-12-28
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多