swift - 可选类型 'Bool' 不能用作 bool 值;改为测试 '!=nil'

标签 swift option-type

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

enter image description here

我在第一个 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.")
}

最佳答案

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

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

关于swift - 可选类型 'Bool' 不能用作 bool 值;改为测试 '!=nil',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30970359/

相关文章:

java - 删除 map 条目会导致 map 条目中的对象引用可选更改

swift - 变量 'xxx' 从未发生突变,请考虑更改为 'let'

Java Optional.ofNullable 与 Mockito

ios - 使用 'if let' 在 swift 'where' 中仅处理其他条件

ios - 如何获得由正则表达式分隔的组件,但也有分隔符?

swift - 垂直滚动 NSTableView Swift macOS 中的水平 NSCollectionView

swift - 我如何在 WordPress REST API(版本 2)中创建评论

arrays - 数组或字典扩展中的 Swift 3.0 : compiler error when calling global func min<T>(T, T)

PHPDoc 可选参数

ios - 缓慢的场景转换