views:

117

answers:

1

Hi everyone,

I have a Core Data entity which needs a gender property. I just need one of two, the standard male or female values. What's the best practice for this in Core Data?

In the .NET world with databases I would've created a Gender table, with foreign key in the child table. I'm still getting my head around Core Data right now - any suggestions would be greatly appreciated.

Cheers, Dany.

UPDATE

Based on the comments here I have added an extra NSString property in my Core Data entity called gender. Its getter and setter manipulate the isMale property value. The UI is bound to gender - works a treat so far! Thanks for the help everyone.

+1  A: 

I would not do a look up as Core Data is not a database but rather an object hierarchy that happens to persist to a database.

Instead I would have a boolean called male with a getter accessor -isMale since there is no risk of there ever being a third.

update

Despite the commentary, how you interact on the UI is completely separate from how you store the data. How you display is up to your UI design. The code in your controller will handle the translation between the boolean state and the UI display.

You can have a checkbox, radio buttons, drop down list, et. al.; doesn't matter. Just translate what the user interacts with in your controller.

Marcus S. Zarra
Actually, that completely depends on the industry. Plenty of them must differentiate a third state at least. :-)
Joshua Nozzi
our database has 4, Male, Female, Unknown and Change in Progress.
kubi
@kubi: lol. Anyway, good point; if you use the word "ever" (or derivatives like "never"), you are probably about to make a mistake.
Williham Totland
The question clearly states that only two values are needed so boolean will work. In other situations, you may need to use an integer and a enum of constants. You would rarely need a user (runtime) definable category.
gerry3
Ah...yes...I thought of that briefly. I'm guessing I can just do a value transformer for display purposes. How do I handle data entry side of things? I don't quite like the idea of a checkbox for "Is Male?" question. Would be nice if I can give them the two options, Male or Female, in a combo box.
DanyW
Thanks for the update. Now I have to figure out how to wire this up to the UI. I guess that's an entirely different question.
DanyW