CoreData和NSPredicate

时间:2013-08-01 12:46:20

标签: ios database sqlite core-data nspredicate

我的iOS应用程序上有coredata,但我正在做的是从表中收集所有值,在我的例子中是一个Questions表,其中包含以下内容:

@property (nonatomic, retain) NSString * answerA;
@property (nonatomic, retain) NSString * answerB;
@property (nonatomic, retain) NSString * answerC;
@property (nonatomic, retain) NSString * answerD;
@property (nonatomic, retain) NSString * correctAnswer;
@property (nonatomic, retain) NSString * question;
@property (nonatomic, retain) NSString * timeStampLastUsed;
@property (nonatomic, retain) NSNumber * gameType;
@property (nonatomic, retain) NSString * continent;

我以这种方式获得所有价值:

NSError *error;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Questions" inManagedObjectContext:managedObjectContext];

    [fetchRequest setEntity:entity];

    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

我想要的是在NSArray * fetchedObjects中只有具有特定游戏类型的问题。

我怎样才能使用NSPredicate?

由于 最诚挚的问候

1 个答案:

答案 0 :(得分:1)

如果你知道你正在寻找的游戏类型的价值,你可以使用这样的东西:

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"gameType == %i", 0]];

通过在获取请求中设置谓词,它会过滤掉结果并仅返回您感兴趣的项目。