简体   繁体   中英

Searching an NSArray full of dictionary objects not working

I am trying to search an nsarray with dictionary objects but the array is unchanged even after filtering it with the predicate that I have set up. Thanks in advance for any help at all.

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self filterArray:searchText];
    NSLog(@"search text changed");
}


- (void)filterArray: (NSString *)searchText
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(userSetname LIKE[cd] %@)", searchText];

    self.originalResults = self.freindResults;
    NSMutableArray *array = self.freindResults;

    [array filteredArrayUsingPredicate:predicate];
    NSLog(@"Filtered array %@", array);
    self.freindResults = array;
    [self.tableView reloadData];
}

What I generally have as a dictionary is:

"<Freinds:4CXI96JMLU:(null)> {
ID = 4CXI96JMLU;
name = "Kevin Turner";
profilePic = "<PFFile: 0x10a290220>";
user = h6r0fwqau9xzi14ojivjpu9f;
userID = h6r0fwqau9xzi14ojivjpu9f;
userSetname = kevonturner;
}",
"<Freinds:YtTGlssOHv:(null)> {
ID = YtTGlssOHv;    
name = "John Appleseed";
profilePic = "<PFFile: 0x10a265360>";
user = h6r0fwqau9xzi14ojivjpu9f;
userID = ozu5st58546zd6nucn8gzutmo;
userSetname = johnnyy;
}"

as outputted in the console

Check this,

NSPredicate *filter = [NSPredicate predicateWithFormat:@"userSetname = %@",name];

NSArray *filteredArry = [yourArray filteredArrayUsingPredicate:filter];

You will not get filtered data from the array instance that you have.. filteredArrayUsingPredicate will return the filtered array. change the code as follows..

- (void)filterArray: (NSString *)searchText

{ NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(userSetname LIKE[cd] %@)", searchText];

self.originalResults = self.freindResults;
NSMutableArray *array = self.freindResults;

NSArray *filteredArry = [array filteredArrayUsingPredicate:predicate];
NSLog(@"Filtered array %@", filteredArry);

self.freindResults = filteredArry;
[self.tableView reloadData];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM