使用核心数据实体作为枚举?
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
questionType
必须是一个对象吗?如果您想使用枚举,只需将Question
实体的questionType
属性声明为整数,而不是像QuestionType
这样的其他实体。或者,您可以将
questionType
属性声明为字符串,并直接将typeName
保留在那里。即使您使用枚举,语法也不像 C / Objective-C 中的
EnumName.EnumKind
。有关语法,请参阅任何教科书。如果您继续使用
questionType
作为实体,我建议您将获取的结果缓存在字典中,如:之类的。
Does
questionType
have to be an object? If you want to use an enum, you can just declare yourquestionType
property of theQuestion
entity to be an integer, not another entity likeQuestionType
.Or, you can declare your
questionType
property to be a string, and directly keeptypeName
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:and something like that.