【问题标题】:optional type 'Bool' cannot be used as a boolean; Test for '!=nil' instead可选类型 'Bool' 不能用作布尔值;改为测试 '!=nil'
【发布时间】:2015-06-21 23:34:04
【问题描述】:

可选类型 'Bool' 不能用作布尔值;改为测试 '!=nil'

我在第一次 if 时遇到错误,通过替换 if 条件 (after),第二个 if 条件 从未运行。任何想法?

之前:

if(userEmail?.isEmpty || userPassword?.isEmpty || userRepeatPassword?.isEmpty){
      displayMyAlertMessage("All fields are required")
      return
}

if(userPassword != userRepeatPassword){
      displayMyAlertMessage("Passwords do not match.")
}

之后:

if(userEmail != nil || userPassword != nil || userRepeatPassword != nil){
      displayMyAlertMessage("All fields are required")
      return
}

if(userPassword != userRepeatPassword){
      displayMyAlertMessage("Passwords do not match.")
}

【问题讨论】:

    标签: swift optional


    【解决方案1】:

    您正在检查值是否与 nil 不同,如果不同则返回,根据您的 cmets 和您的第二个,如果您可能想检查它是否为 nil。

    println("Checking login details")
    if(userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty){
          displayMyAlertMessage("All fields are required")
          println("Fail to login not all fields where fill") 
          return
    } else if(userPassword != userRepeatPassword){
          displayMyAlertMessage("Passwords do not match.")
          println("Fail to login password does not match") 
    } else {  
      var uiAlert = UIAlertController(title: "Alert", message: "Registration was successful", preferredStyle: UIAlertControllerStyle.Alert)
      self.presentViewController(uiAlert, animated: true, completion: nil)
      uiAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
       dismissViewControllerAnimated(true, completion:nil)
      }))
    }
    

    【讨论】:

    • 我也试过了,但是页面会跳转到另一个页面,什么都不做
    • @GhasemTabatabaei 我添加了一个 println 来帮助调试。你能告诉我们你的控制台打印了什么吗?
    • @GhasemTabatabaei 我更新了答案以匹配您的原始设计,在用户单击确定后显示警报并关闭模式,小心将视图推到另一个顶部而不关闭,因为您可以拥有 stackoverflow。希望对您的应用有所帮助并祝您好运!
    • @GhasemTabatabaei 我只看到你的其余代码它不应该是 nil 而只是一个空字符串“”我修复了它现在应该可以工作的答案
    • @GhasemTabatabaei 我总是在帮助别人时学到新东西!很高兴现在一切正常!祝你的应用顺利完成!
    【解决方案2】:

    你需要用!而不是?来包裹它。
    这将解决错误消息:

    if (username!.isEmpty) .....
    

    【讨论】:

      【解决方案3】:
      if(userEmail!.isEmpty || userPassword!.isEmpty || userRepeatPassword!.isEmpty)
      

      您不能检查可选的值,因为您可能已经知道,作为可选意味着它可以存在或不存在。一种解决方案称为force unwrapping,它是通过使用“!”来完成的。 (感叹号)。 “?” (问号)只是让编译器知道可能是也可能不是一个值,所以使用“!”我们告诉编译器我们知道它可能是也可能不是该变量中的一个值但是我们知道会有一个值,即使它是一个空字符串,这与其他编程不同考虑空字符串或空数组(如“false”)的语言。 swift 中并非如此。

      条件语句中的表达式必须是有效的布尔结果。

      【讨论】:

      • 虽然此代码可以回答问题,但提供有关 如何 和/或 为什么 解决问题的附加上下文将改善答案的长期价值。
      • 我赶时间,所以我不得不照原样发布或关闭浏览器。
      • @edduvs 我在寻找“?”的区别和 ”!”。很有帮助。谢谢。
      • @GhasemTabatabaei 很高兴我能提供帮助。
      【解决方案4】:

      使用可选的布尔值,它的工作方式略有不同,您需要明确检查该值是否为 nil。这就是为什么你的After: 有效,而不是你的Before:

      //now this is checking if your values are nil (empty) rather than not nil
      //before if a user had valid fields, it would say that "All fields are required"
      //now, this will work
      if(userEmail == nil || userPassword == nil || userRepeatPassword == nil){
        displayMyAlertMessage("All fields are required")
      } else if(userPassword != userRepeatPassword){
        displayMyAlertMessage("Passwords do not match.")
      } else {
        //success
        //perform segue here to correct screen
      }
      

      有几个选项可以用来执行 segue,我将选择使用 presentViewController 方法,请参阅下文了解如何集成它。 ...

      else {
        //success
        //perform segue here to correct screen
        presentViewController(yourMainScreenViewController, animated: true, completion: nil)
      }
      

      您也可以使用 performSegueWithIdentifier("yourMainScreenIdentifier", sender: nil) 如果你不使用presentViewController 方法,比如:

      else {
        //success
        //perform segue here to correct screen
        performSegueWithIdentifier("yourMainScreenIdentifier", sender: nil)
      }
      

      我将添加我认为您的displayMyAlertMessage 是:

      func displayMyAlertMessage(alert: String) {
        println(alert)
      }
      

      【讨论】:

      • 我也试过了,但是页面会跳转到另一个页面,没有做任何事情!你知道是什么问题吗?
      • 跳转到另一个页面是什么意思?你能分享更多跳转到其他页面的代码吗? @GhasemTabatabaei
      • 我的意思是,从注册页面会跳回上一页(登录)..(请看我刚刚添加的图片)。
      • 查看我上面的代码,我已经编辑了它,我想你要做的是以编程方式执行一个 segue,并添加和else 语句,以便如果用户成功输入凭据,它会继续到主屏幕。 @GhasemTabatabaei
      • 没问题!很高兴提供帮助:) @GhasemTabatabaei
      猜你喜欢
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 2019-12-11
      • 2018-07-23
      • 1970-01-01
      • 2021-12-10
      相关资源
      最近更新 更多