Orkhan Alizade
Orkhan Alizade

Reputation: 7549

How to search inside of Dictionary in Swift

In my UITableView I have cells with avatar image and name surname(UIImage and Label). And I want to search inside of my UITableView. So, when I type my user's name or surname it would show user's name surname with the avatar image.

Now, I can search just inside of an array with the next code:

var rosters = [String: UIImage]()
var displayNames = [String]()
var filteredTableData = [String]()

// Here I remove all elements from the array and add to it user display name and avatar
rosters.removeAll(keepCapacity: false)
for user in OneRoster.buddyList.fetchedObjects! {
  var roster = OneRoster.userFromRosterForJID(jid: user.jidStr)
  var image = OneChat.sharedInstance.xmppvCardAvatarModule?.photoDataForJID(roster?.jid)

  rosters[user.displayName] = UIImage(data: image!)!
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> onlineUserCell {
    let cell:onlineUserCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! onlineUserCell

    if (self.resultSearchController.active) {
      cell.username.text = filteredTableData[indexPath.row]
      //cell.avatarImage.image = images[indexPath.row]

      return cell
    }
}


func updateSearchResultsForSearchController(searchController: UISearchController)
    {
        filteredTableData.removeAll(keepCapacity: false)
        displayNames.removeAll(keepCapacity: false)

        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)

        for (name, image) in rosters {
            displayNames.append(name)

            let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
            let array = (displayNames as NSArray).filteredArrayUsingPredicate(searchPredicate)
            filteredTableData = array as! [String]
        }

        let array = (displayNames as NSArray).filteredArrayUsingPredicate(searchPredicate)
        filteredTableData = array as! [String]

        self.tableView.reloadData()
    }

So, this code searches just among name surname, without avatar image. I thought, that I can use dictionary, as:

var users = [UIImage: String]()

and later search by String and show as UIImage and String together in the cell. But I searched a lot and couldn't found how to search, that later I can search by String and to get result with UIImage, too.

Any ideas?

Upvotes: 2

Views: 3726

Answers (1)

Moriya
Moriya

Reputation: 7906

Searching the dictionary could be done by just accessing the values from the dictionary and searching them like

var users = [UIImage: String]()
let values = Array(users.values)

However this use of dictionary seems a bit wrong as you will never look up a value based on the UIImage I guess.

What you should use instead then would be a tuple or a class that holds both UIImage and String

var users = [(image:UIImage,name:String)]()

you can then simply filter the values like

users.filter { (element:(image:UIImage, name:String)) -> Bool in
    return element.name.containsString(searchfrase)
}

Upvotes: 3

Related Questions