可选类型'Bool'不能用作布尔值;测试'!= nil'代替 [英] optional type 'Bool' cannot be used as a boolean; Test for '!=nil' instead

查看:423
本文介绍了可选类型'Bool'不能用作布尔值;测试'!= nil'代替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我第一次出现 if 时出错,方法是替换if条件(after),而 second if condition 从未运行.任何的想法?

I got an error at first if, by replacing the if condition (after), the second if condition did never run. Any idea?

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不同,如果您想检查它是否为nil,则根据第二个注释

You are checking if the value is different of nil and returning if it is, based un your comments and your second if you probably want to check if it is 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)
  }))
}

这篇关于可选类型'Bool'不能用作布尔值;测试'!= nil'代替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆