搜索充满字典对象的NSArray不起作用

时间:2014-01-29 05:27:06

标签: iphone objective-c nsarray nsdictionary nspredicate

我正在尝试使用字典对象搜索nsarray,但即使在使用我设置的谓词对其进行过滤后,该数组仍未更改。提前感谢您提供任何帮助。

 - (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];
}

我通常拥有的字典是:

"<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;
}"

在控制台中输出

2 个答案:

答案 0 :(得分:0)

检查一下,

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

NSArray *filteredArry = [yourArray filteredArrayUsingPredicate:filter];

答案 1 :(得分:0)

您不会从您拥有的数组实例中获取过滤数据.. filteredArrayUsingPredicate将返回已过滤的数组。更改代码如下..

- (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];

}