使用核心数据实体作为枚举?

发布于 2024-10-05 16:12:14 字数 1293 浏览 3 评论 0原文

我有 2 个核心数据实体:Question 和 QuestionType。每个问题都有 1 个 QuestionType。

QuestionType 有一个 typeName 字符串属性;这主要是我识别问题类型的方式。它被固定为几种不同类型的列表。我想知道是否可以使用数据中所有 QuestionType 的列表作为枚举,或者如果不能,使用此列表将 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:

I have 2 core data entities: Question, and QuestionType. Every Question has exactly 1 QuestionType.

QuestionType has a typeName string attribute; which is primarily how I identify which QuestionType it is. It is fixed to a list of a few different types. I am wondering if it is possible to use the list of all the QuestionTypes in the data as an enum, or if not, what's the best way to use this list to assign a QuestionType to a Question, and check the QuestionType later?

Currently, when I want to assign a type to a question (based on knowing the typeName), I am doing this:

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];

This seems like a lot of work just to assign a QuestionType to my Question! And I have to repeat this for other similar entities.

And then when I want to check the QuestionType later, I am doing:

 if ([question.questionType.typeName isEqualToString:@"text"]){

This works fine, but I feel like I should be comparing the question.questionType to the specific QuestionType I am looking for, as opposed to just comparing the typeName.

Is there any way to set up an enum to hold my QuestionTypes, so that I can do this:

question.questionType = Text;
switch(question.questionType)
{
    case Text:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2024-10-12 16:12:14

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.
            }
        }
   }

之类的。

Does questionType have to be an object? If you want to use an enum, you can just declare your questionType property of the Question entity to be an integer, not another entity like QuestionType.

Or, you can declare your questionType property to be a string, and directly keep typeName there.

Even when you use an enum, the syntax is not like EnumName.EnumKind in C / Objective-C. See any textbook for the syntax.

If you keep using questionType as an entity, I would suggest you to cache the results of the fetch in the dictionary, as in:

   (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.
            }
        }
   }

and something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文