0

在我的数据模型中,我有:

Activity(name, sponsor, Location*) // one activity may have many Locations
Location(name, street, suburb, state, postcode)

而且我需要一种方法来获取具有指定位置名称的活动,例如,用户想知道“ICT Building”中的所有活动,那么如何构造查询的谓词?

就像是:

"Location.name == ICT Building" ?

谢谢!

4

2 回答 2

0

managedObjectContext在您的上下文中使用它:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
    entityForName:@"Activity" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSString *locationName = @"ICT Building";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"Location.name CONTAINS %@", locationName];
[fetchRequest setPredicate:pred];
NSError *error = nil;
NSArray *objects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

这将返回一个包含托管对象的数组,其中名称为 ICT Building。

于 2012-07-06T01:04:44.230 回答
0

我终于弄明白了。改变

@"Location.name == %@"

@"Location.name CONTAINS %@"

在@CoreCode 的回答中。

于 2012-07-06T02:26:41.510 回答