【问题标题】:Function with same name as property swift: .first(where: ...)与属性 swift 同名的函数:.first(where: ...)
【发布时间】:2016-12-14 00:12:48
【问题描述】:

对于var myArray:[[String:Any]] Xcode 让我自动完成

 myArray.first(where:{$0["name"] == "John Doe"})

但随后不允许我编译它并出现错误“无法调用非函数类型的值'[String:Any]?'”

换句话说,swift 无法弄清楚我正在寻找方法first(where:_) 而不是无参数版本.first。什么给了?

【问题讨论】:

  • @AlexanderMomchliov 修复了它!

标签: swift xcode swift3


【解决方案1】:

Xcode 针对此错误给出了错误消息。这是一个简单的类型的事情。您无法将 AnyString 进行比较

 myArray.first(where:{$0["name"] == "John Doe"})

应该是:

  myArray.first(where:{
     guard let name $0["name"] as? String else {return false}
     return name == "John Doe"
  })

【讨论】:

  • 你可以写成myArray.first(where:{$0["name"] as? String == "John Doe"})
  • @JoeDaniels 这就是为什么你不使用字典来存储具有像这样的静态键的数据结构:p
猜你喜欢
  • 1970-01-01
  • 2017-11-04
  • 2014-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多