Search Through Core Data
All,
I have a core data entity, CreditCard
, and each entity has four values.
So, what I need to do is search through ALL of my CreditCard
entities and find the one with a property pocketNum
that is equal to whate开发者_如何学Cver value the user puts in, and then return it so I can pull necessary values from it.
How would I do this?
Thanks, James
Try NSFetchRequest
with NSPredicate
:
// 'moc' is your NSManagedObjectContext instance
// 'yourPockerNum' is what you want to find
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CreditCard" inManagedObjectContext:moc]
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = entity;
request.resultType = NSDictionaryResultType;
request.predicate = [NSPredicate predicateWithFormat:@"pocketNum == %@", yourPocketNum];
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
// do something with results
精彩评论