【问题标题】:binary operator '==' cannot be applied in swift 3 [duplicate]二元运算符'=='不能应用于swift 3 [重复]
【发布时间】:2018-01-22 11:08:12
【问题描述】:

在 Swift 3 中,当我尝试比较两个项目时出现错误。

    var userData = NSDictionary()




 if !(self.userData.count == 0) && (self.userData["user_status"] == "1") {

                }

错误提示:二元运算符 '==' 不能应用于类型为 'Any? 的操作数;和'字符串'

在 Swift 3 中执行此操作的正确方法是什么?

【问题讨论】:

  • 我这段时间大概写了 20 行 swift,但问题显然是你试图比较 2 种不同的类型,而 swift 不知道该怎么做。
  • 是的,我正在尝试比较 NSDictionary 和 string 。在这里我收到错误:“(self.userData [“user_status”] ==“1”)“
  • self.userData["user_status"] 返回什么?

标签: swift3


【解决方案1】:

你应该这样做:

var userData = [String : String]()
userData["user_status"] = "1"
if !(userData.count == 0) && (userData["user_status"] == "1") {
// do something here
print ("hello")
}

【讨论】:

  • 数据可能来自他无法直接控制的东西。请看上面的重复置顶
猜你喜欢
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
相关资源
最近更新 更多