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

optional type 'Bool' cannot be used as a boolean; Test for '!=nil' instead

optional type 'Bool' cannot be used as a boolean; Test for '!=nil' instead

我一开始就报错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.")
}

您正在检查该值是否与 nil 不同,如果是,则返回,基于您的评论和您的第二个,如果您可能想检查它是否为 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)
  }))
}

对于可选的布尔值,它的工作方式略有不同,您需要明确检查该值是否为 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)
}

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

if (username!.isEmpty) .....
if(userEmail!.isEmpty || userPassword!.isEmpty || userRepeatPassword!.isEmpty)

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

不是这种情况

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