-2

In Swift 4, I'm trying to compare the length of the text of an UISearchBar textfield with a minimum length:

 @objc var arrRes = [[String:AnyObject]]()
 var searcharr = [String]()

private func searchBar( searchBar: UISearchBar,  textDidChange searchText: [[String:AnyObject]] )
{
    searcharr = arrRes.filter({$0.prefix(searchText.count) == searchText})
}

But I am getting the error:

Binary operator '==' cannot be applied to operands of type 'Slice<Dictionary>' and '[[String : AnyObject]]'

Nishad
  • 203
  • 1
  • 2
  • 9
  • `arrRes` seems to be an array of dictionary. You probably want to compare `searchText` against a specific value in each dictionary. – rmaddy Jul 21 '18 at 15:36
  • You need to clearly explain how you want the dictionaries in `arrRes` to be searched so you end up with an array of `String`. And that makes no sense anyway. Why don't you want to end up with a filtered array of dictionary? – rmaddy Jul 21 '18 at 16:05
  • SearchText also changed from string to array , what the requirement then , also you should be clear from the beginning of what you want to achieve – Shehata Gamal Jul 21 '18 at 16:18
  • Apart from the other issues the (array) type of `searcharr` must be the same as `arrRes`. – vadian Jul 21 '18 at 16:30

1 Answers1

0

Correct callback

func searchBar(_ searchBar: UISearchBar,  textDidChange searchText:String) {
   searcharr = arrRes.keys.filter{(arrRes[$0] as? String)?.hasPrefix(searchText) ?? false }
}

//

let result =  arrRes.filter {  
  let dic = $0       
  return $0.keys.filter { (dic[$0] as? String )?.hasPrefix(searchText)  ?? false }.count != 0 
}
print(result)
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • same problem .. because i called ` @objc var arrRes = [[String:AnyObject]]() ` & ` var searcharr = [[String:AnyObject]]() ` – Nishad Jul 21 '18 at 15:35
  • what content of the arrRes , post it's declaration – Shehata Gamal Jul 21 '18 at 15:36
  • it shows error **Cannot convert value of type 'String' to expected argument type '(AnyObject) throws -> Bool'** at **swift 4** – Nishad Jul 21 '18 at 15:41
  • not working now error is **Value of type '[[String : AnyObject]]' has no member 'keys'** – Nishad Jul 21 '18 at 15:48
  • please change the signature of the search bar method as in answer , and post the type of **arrRes ** as from the error it seems it's a dictionary – Shehata Gamal Jul 21 '18 at 15:50
  • Based on the edited question, this isn't right either. It's a really poor question with no details. This is all just guessing until the OP clarifies the requirement. – rmaddy Jul 21 '18 at 16:06
  • @rmaddy I agree , the op changed the requirement from dictionary to array – Shehata Gamal Jul 21 '18 at 16:07
  • not working now error is Value of type '[[String : AnyObject]]' has no member 'keys' ?? – Krunal Nagvadia Mar 13 '19 at 14:58