Function with same name as property swift: .first(where: ...)

253 views Asked by At

For var myArray:[[String:Any]] Xcode lets me autocomplete

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

but then doesn't let me compile it with an error "Cannot call value of non-function type '[String:Any]?'"

So anotherwords, swift can't figure out that I'm looking for the method first(where:_) instead of the no param version .first. What gives?

1

There are 1 answers

2
Joe Daniels On

Xcode was giving the wrong message for this error. it was a simple type thing. You can't compare an Any to a String

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

should have been:

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