使用NSPredicate搜索NSFetchedResultsController [英] Search NSFetchedResultsController using NSPredicate

查看:107
本文介绍了使用NSPredicate搜索NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用Core Data,并且相对来说,新的iOS编码,我有一些麻烦工作如何搜索我的fetchedResultsController过滤出用户输入到searchBar的字符串。



我知道如何从searchBar获取字符串以及如何调用我的fetchResultsController方法。但是我所努力的是如何使用NSPredicate只匹配使用核心数据存储的行与在searchBar中键入的行。



在 - (NSFetchedResultsController *)feticedResultsController



中执行此操作(我按字母顺序切换,只是为了表明自己正在工作)。


$ b b

  if(!isSearching){
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@LabelValueascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];

[fetchRequest setSortDescriptors:sortDescriptors];

[sortDescriptor release];
[sortDescriptors release];
}

else {
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@LabelValueascending:NO];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];

NSLog(@%@,searchBar.text);

/ * NSPredicate * predicate = [NSPredicate predicateWithFormat:@ANY contains [cd]%@,searchBar.text];
[fetchRequest setPredicate:predicate]; * /

[fetchRequest setSortDescriptors:sortDescriptors];

[sortDescriptor release];
[sortDescriptors release];
}



我不太明白如何使用谓词来过滤我的数据。注释掉的代码在取消注释时会导致NSException:

  ***由于未捕获异常而终止应用程序'NSInvalidArgumentException'无法解析格式字符串ANY contains [cd]%@'

解决方案,但我非常感谢您能够提供的任何帮助。感谢

解决方案

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@ANY< yourFieldNameHere> contains [cd]%@,searchBar.text]; 


I've been stuck on this problem for a while now and have tried my best to work out a solution.

I am using Core Data and, being relatively new to iOS coding, I'm having some trouble working out how to search my fetchedResultsController to filter out the string that the user types into the searchBar.

I know how to get the string from the searchBar and how to call my fetchResultsController method. But what I'm struggling with is how to use NSPredicate to match only the rows stored in using core data with that typed in the searchBar.

At the moment I'm doing this in - (NSFetchedResultsController *)fetchedResultsController

(I switch the sort alphabetically just to show myself that it's working).

if (!isSearching) {
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"LabelValue" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    [sortDescriptor release];
    [sortDescriptors release];
}

else {
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"LabelValue" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    NSLog(@"%@", searchBar.text);

    /*NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY contains[cd] %@", searchBar.text];
    [fetchRequest setPredicate:predicate];*/

    [fetchRequest setSortDescriptors:sortDescriptors];

    [sortDescriptor release];
    [sortDescriptors release];
}

I don't quite understand how to use the predicate to filter my data. The commented out code causes an NSException when uncommented:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "ANY contains[cd] %@"'

It's probably quite a simple solution, but I'd be very grateful for any help you are able to offer. Thanks

解决方案

   

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY <yourFieldNameHere> contains[cd] %@", searchBar.text];

这篇关于使用NSPredicate搜索NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆