你好我有Contactarray (
{
"firstNAme"="name1"
"lastName"="name2"
"phoneNumber"="12345678902";
}
{
"firstNAme"="name1"
"lastName"="name2"
"phoneNumber"="12345678902";
}
喜欢这个
UITextField
我想在UItableView
中输入人名时搜索此人。然后应加载已过滤的NSMutableArray
。此NSMutableDictionaries
包含UITableview
。
如何从这些对象中找到匹配的对象?
让我们说我想搜索所有name1人。然后我想找到包含“name1”的所有对象,这些对象应填充到另一个数组以加载<__NSArrayI 0x7b601f70>(
firstname = Kate;
lastName = Bell;
phone = "(415) 555-3695";
userimg = "<UIImage: 0x7b67b3a0>";
};
{
firstname = Kate;
lastName = Bell;
phone = "(415) 555-3695";
userimg = "<UIImage: 0x7b67b3a0>";
},
请帮帮我。 感谢。
更新
这是我的联系人数组
[playlistArray removeAllObjects];
NSArray *contacts=[[NSArray alloc] initWithArray:mutArraySearchContacts];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"firstname = %@ OR lastName = %@",currentSrchStr,currentSrchStr];
playlistArray=[contacts filteredArrayUsingPredicate:filter];
[self performSelectorInBackground:@selector(playlistsLoaded) withObject:nil];
这是我的搜索代码
`
(lldb) po playlistArray
<__NSArrayI 0x7b74adf0>(
)
`
但我的播放列表是空的。
`
val()
`
我在这里做错了什么?
答案 0 :(得分:0)
您可以使用keysOfEntriesPassingTest:
方法查找值等于@"test"
的所有键。
在下面的实现中,只会找到第一个键。如果您需要对象为@"Test"
的所有密钥,请不要指定*stop
。
NSString *target = @"test";
NSSet *keys = [myDictionary keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop)
{
return (*stop = [target isEqual:obj]);
}];
当你拿到钥匙时,你可以继续前进。
答案 1 :(得分:0)
NSArray *contacts = ...; //your array of NSDictionary objects
NSPredicate *filter = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"firstName == %@", @"name1"]];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:filter];
如果您需要搜索更多条件,例如全名:
NSPredicate *filter = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"firstName == %@ OR lastName == %@" ,@"name1", @"name2"]];
以下是Predicate Programming Guide,其中说明了更高级的功能。
答案 2 :(得分:0)
使用NSPredicate
时无需迭代。试试这个。
NSPredicate * myPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"SELF['firstNAme'] contains '%@' || SELF['lastName'] contains '%@'",currentSrchStr,currentSrchStr]];
NSArray *filterArray = [mutArraySearchContacts filteredArrayUsingPredicate:myPredicate];
NSLog(@"filterArray %@", filterArray);
更新1:
使用CONTAIN[C]
修改谓词以区分大小写,例如
NSPredicate * myPredicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"SELF['firstNAme'] CONTAINS[c] '%@' || SELF['lastName'] CONTAINS[c] '%@'",currentSrchStr,currentSrchStr]];
希望这可以帮助你!!