当前位置:编程学习 > wap >>

objective-C IOS core data 使用NSPredicate查询

一个表里面有firstname ,lastname两个属性,我想查出firstname或者lastname中包含form的项
分别查我会,不知道怎么合并

NSString * searchTerm = [searchBar text];
    [NSFetchedResultsController deleteCacheWithName:@"Master"];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];
    NSString *form = [NSString stringWithFormat:@"%@*",searchTerm ];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstname like %@)",form];
    //    NSLog(@"%@",searchBar.text);
    [fetchRequest setPredicate:predicate];
    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstname" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
--------------------编程问答--------------------

我也不会。。。

也希望有详细的解答 --------------------编程问答-------------------- --------------------编程问答--------------------
补充:移动开发 ,  iPhone
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,