【问题标题】:Using a core data entity as an enum?使用核心数据实体作为枚举?
【发布时间】: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


    【解决方案1】:

    questionType 必须是一个对象吗?如果您想使用枚举,您可以将Question 实体的questionType 属性声明为整数,而不是像QuestionType 这样的另一个实体。

    或者,您可以将questionType 属性声明为字符串,并直接将typeName 保留在那里。

    即使你使用枚举,语法也不像 C / Objective-C 中的 EnumName.EnumKind。有关语法,请参阅任何教科书。

    如果你继续使用questionType作为一个实体,我建议你将获取的结果缓存在字典中,如下:

       (QuestionType*)questionTypeWithName:(NSString*)name 
       {
            static NSMutableDictionary*dict=nil;
            if(!dict){
                 dict=[[NSMutableDictionary alloc] init]];
            }
            QuestionType*qt=[dict objectForKey:name];
            if(qt){
                  return qt;
            }else{
                NSFetchRequest *questionTypeFetchRequest = [[NSFetchRequest alloc] init];
                    ...
                NSArray*result = ... executeFetchRequest: ...
                if(result){
                      ...
                      add the resulting qt to the dict ... 
                      ...
                }else{
                      create a new QuestionType entity with a given name
                      add it to the dict.
                      return it.
                }
            }
       }
    

    诸如此类。

    【讨论】:

    • 谢谢!更新以修复枚举语法。它必须是 managedObject,因为可能的 questionTypes 列表应该能够通过重新编译以外的其他方式进行更新,并且因为它可能包含除 typeName 之外的其他属性。但我相信静态词典会给我我想要的。
    • 刚刚意识到一个问题......虽然这对于设置 questionType 非常有用,但我认为它不适用于比较它。 question.questionType 与我在字典中的 questionType 对象不同;因为它将是从 CoreData 获取的新对象。我可以实现某种等于:在 QuestionType 上返回 true 的 typeName 方法是否相同?
    • 我相信可以保证数据库中的一个条目由NSManagedObject 中的一个NSManagedObject 表示,因此您只需比较指针即可。当然,如果您有多个QuestionType 和相同的typeName,则它们算作多个对象。但是你不应该创建多个;你应该始终保证它是独一无二的。
    • 我相应地更改了上面的代码,以便在必要时创建QuestionType
    • 啊,我没有意识到对象肯定是一样的。这应该工作得很好。一开始实施会有点痛苦;但是一旦到位,它将使代码更具可读性和编写速度。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多