objective-C IOS core data 使用NSPredicate查询

zhanghanivy 2012-03-29 03:14:19
一个表里面有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;
...全文
490 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
相奇 2012-03-29
  • 打赏
  • 举报
回复


我也不会。。。

也希望有详细的解答
Core Data is intricate, powerful, and necessary. Discover the powerful capabilities integrated into Core Data, and how to use Core Data in your iOS and OS X projects. All examples are current for OS X El Capitan, iOS 9, and the latest release of Core Data. All the code is written in Swift, including numerous examples of how best to integrate Core Data with Apple's newest programming language. Core Data expert Marcus Zarra walks you through a fully developed application based around the Core Data APIs. You'll build on this application throughout the book, learning key Core Data elements such as NSPredicate, NSFetchRequest, thread management, and memory management. Start with the basics of Core Data and learn how to use it to develop your application. Then delve deep into the API details. Explore how to get Core Data integrated into your application properly, and work with this flexible API to create convenience methods to improve your application's maintainability. Reduce your migration difficulties, integrate your Core Data app with iCloud and Watch Kit, and use Core Data in a queue-based environment. By the end of the book, you'll have built a full-featured application, gained a complete understanding of Core Data, and learned how to integrate your application into the iPhone/iPad platform. This book is based on Core Data in Objective-C, Third Edition. It focuses on Swift and adds an additional chapter on how to integrate Core Data with an efficient network implementation, with best practices on how to load and pre-load data into your Swift application. What You Need: Mac OS X El Capitan and iOS 9 and a basic working knowledge of Swift Table of Contents Chapter 1. Before We Begin Chapter 2. Under the Hood Chapter 3. iOS: NSFetchedResultsController Chapter 4. Versioning and Migration Chapter 5. Performance Tuning Chapter 6. Threading Chapter 7. Bulk Changes Chapter 8. Network Operations Chapter 9. Using Core Data with iCloud Chapter 10. Adding a Desktop Foundation Chapter 11. Bindings, KVC, and KVO Chapter 12. Spotlight, Quick Look, and Core Data

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧