【发布时间】:2010-12-02 19:59:18
【问题描述】:
我有 2 个核心数据实体:Question 和 QuestionType。每个问题恰好有 1 个 QuestionType。
QuestionType 有一个 typeName 字符串属性;这主要是我如何识别它是哪个 QuestionType。它被固定为几个不同类型的列表。我想知道是否可以将数据中所有 QuestionTypes 的列表用作枚举,或者如果没有,使用此列表将 QuestionType 分配给 Question 并稍后检查 QuestionType 的最佳方法是什么?
目前,当我想为问题分配类型时(基于知道 typeName),我正在这样做:
NSFetchRequest *questionTypeFetchRequest = [[NSFetchRequest alloc] init];
questionTypeFetchRequest.entity = [NSEntityDescription entityForName:@"QuestionType" inManagedObjectContext:self.managedObjectContext];
NSPredicate *questionTypePredicate = [NSPredicate predicateWithFormat:@"typeName like %@", [questionData objectForKey:@"questionType"]];
questionTypeFetchRequest.predicate = questionTypePredicate;
question.questionType = [[self.managedObjectContext executeFetchRequest:questionTypeFetchRequest error:&error] objectAtIndex:0];
仅仅为我的问题分配一个 QuestionType 似乎需要做很多工作!我必须对其他类似实体重复这一点。
然后当我想稍后检查 QuestionType 时,我正在做:
if ([question.questionType.typeName isEqualToString:@"text"]){
这很好用,但我觉得我应该将 question.questionType 与我正在寻找的特定 QuestionType 进行比较,而不是仅仅比较 typeName。
有没有办法设置一个枚举来保存我的 QuestionTypes,以便我可以这样做:
question.questionType = Text;
switch(question.questionType)
{
case Text:
【问题讨论】:
标签: objective-c core-data ios enums entity-relationship