objective c - Using a core data entity as an enum? -


i have 2 core data entities: question, , questiontype. every question has 1 questiontype.

questiontype has typename string attribute; how identify questiontype is. fixed list of few different types. wondering if possible use list of questiontypes in data enum, or if not, what's best way use list assign questiontype question, , check questiontype later?

currently, when want assign type question (based on knowing typename), doing this:

nsfetchrequest *questiontypefetchrequest = [[nsfetchrequest alloc] init]; questiontypefetchrequest.entity = [nsentitydescription entityforname:@"questiontype" inmanagedobjectcontext:self.managedobjectcontext]; nspredicate *questiontypepredicate = [nspredicate predicatewithformat:@"typename %@", [questiondata objectforkey:@"questiontype"]]; questiontypefetchrequest.predicate = questiontypepredicate; question.questiontype = [[self.managedobjectcontext executefetchrequest:questiontypefetchrequest error:&error] objectatindex:0]; 

this seems lot of work assign questiontype question! , have repeat other similar entities.

and when want check questiontype later, doing:

 if ([question.questiontype.typename isequaltostring:@"text"]){ 

this works fine, feel should comparing question.questiontype specific questiontype looking for, opposed comparing typename.

is there way set enum hold questiontypes, can this:

question.questiontype = text; switch(question.questiontype) {     case text: 

does questiontype have object? if want use enum, can declare questiontype property of question entity integer, not entity questiontype.

or, can declare questiontype property string, , directly keep typename there.

even when use enum, syntax not enumname.enumkind in c / objective-c. see textbook syntax.

if keep using questiontype entity, suggest cache results of fetch in dictionary, 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 resulting qt dict ...                    ...             }else{                   create new questiontype entity given name                   add dict.                   return it.             }         }    } 

and that.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -