Filtering array of dictionaries in Swift
Asked Answered
C

4

7

I have An Array With Dictionaries example :

(
        {
        Email = "[email protected]";
        Name = "Kate Bell";
        Number = "(555) 564-8583";
    },
        {
        Email = "[email protected]";
        Name = "Daniel Higgins";
        Number = "555-478-7672";
    }
)

And i want to filter this dictionary according to Key "Name"

 func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    let predicate = NSPredicate(format:"Name == %@", searchText)
    let filteredArray = (arrContact as NSMutableArray).filteredArrayUsingPredicate(predicate)
    print(filteredArray)
    if(filteredArray.count == 0){
                searchActive = false;
            } else {
                searchActive = true;
            }
            tblData.reloadData()
 }

I am always getting empty array in result from above swift code. Please help me to resolve this issue. Thanks

Clavicle answered 8/4, 2016 at 10:1 Comment(0)
O
16

I suggest using Swift's filter instead:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    let filteredArray = arrContact.filter { $0["Name"] == searchText }
    print(filteredArray)
    if filteredArray.isEmpty {
        searchActive = false
    } else {
        searchActive = true
    }
    tblData.reloadData()
}

And as mentioned by @LeoDabus in his comment, you can even simplify further:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    let filteredArray = arrContact.filter { $0["Name"] == searchText }
    print(filteredArray)
    searchActive = !filteredArray.isEmpty
    tblData.reloadData()
}
Obtund answered 8/4, 2016 at 10:6 Comment(1)
searchActive = !filteredArray.isEmptyPemberton
H
8

Try This for swift 3

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    // Put your key in predicate that is "Name"
    let searchPredicate = NSPredicate(format: "Name CONTAINS[C] %@", searchText)
    let array = (arrContact as NSArray).filtered(using: searchPredicate)

    print ("array = \(array)")

    if(array.count == 0){
        searchActive = false;
    } else {
        searchActive = true;
    }
    self.aTable.reloadData()
}
Hoon answered 26/10, 2016 at 9:38 Comment(3)
@Abhishek Rathore. please let me know if you have any issueHoon
that was an awsome answer even i had the same issue but your answer saved my dayAedile
Not working in Swift 4. Please update or add for Swift 4Dmz
S
3

Swift

Get list of item/array from model class.

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if self.searchBarItems.text == "" {
        self.arrFilter = self.arrayDictionarySubModel
    } else {
        self.arrFilter.removeAll()
        let filteredArray = self.arrayDictionarySubModel.filter { ($0.itemTitle?.contains(self.searchBarItems.text ?? ""))!}
        print("filtered array:- ", filteredArray[0].itemTitle!)
        self.arrFilter = filteredArray
    }
    self.searchItemTableView.reloadData()
}
Seignior answered 7/1, 2020 at 9:35 Comment(0)
C
0

where "name" is the key name

  var namePredicate = NSPredicate(format: "Name like %@",  String(searchText));
        let searchPredicate = NSPredicate(format: "Name CONTAINS[C] %@", searchText)
        arrrDict = self.arrrDict.filter { searchPredicate.evaluate(with: $0) };
       you will get the result in dictionary specialy made to get contats from the phone book
Cryptanalysis answered 31/3, 2017 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.