I have come to a roadblock in my current project. I basically have an app that is much like the Core Data Recipe app... Here is the basic structure I have in my .xcdatamodel
Entity: Restaurant String: name Category: category <---- to-many relationship
Entity: Category String: name Restaurant: restaurant <---- to-many relationship So basically, a Restaurant can have multiple categories... And there are a const number of predefined Categories.. For example: Restaurant: Name: Chili’s Categories: Take Out , Family Dining
“Take Out” and “Family Dining” are 2 of 10 different possible Restaurant Categories. How do I go about doing this.. I have looked at the sqllite database and I have my ZRestaurant and ZCategory table + the join table for both of them... I have data in both...
How do I go about setting my Restaurants Catagory with the different values? and then how do I fetch them back?
Thanks all! Kurt
How do I go about setting my Restaurants Catagory with the different values? and then how do I fetch them back?
The best thing to do is go through the Core Data Tutorial for iPhone, which goes through how to add new managed object instances of an Entity type (in your case, "Restaurant"), set that instance's attributes (e.g., "Restaurant.category") and fetch results.
The tutorial uses an Entity type called "Event" which has date and location attributes, but the ideas are all the same.
OK, After working on this for the past 2 days I finally came up with my solution which was actually a mix between Alex and Wills suggestions... Thank you to both of you!!
Here is what I have...
and then I save like this
And that does it!
Thank you so much for the help you two!!!
-Kurt
You would want to do something like this, instead:
You're not instantiating your
Category
managed object correctly, and you want to learn how to use the accessors. Once you have done that, you will be better able to learn how to do fetches.Honestly, I would recommend putting your project to the side and going through the Core Data Tutorial for iPhone.
If you want to call a method like "-addCatagoryObject:" on your NSManagedObject subclass, you have to have the code for that method in your actual .m file - it is NOT generated at runtime.
HOWEVER, it can be generated for you semi-automatically by Xcode - look for the various menu items that allow you to copy method definitions and implementations in Xcode.
Most people skip these nowadays, you don't NEED to call -addCategoryObject:, you can just let the runtime generate accessor code for you.
First off, your variable name (in the header and in your model) should be "categories", not "category", since it's representing a set, not a singleton.
You can then set categories to any set you want, using something like: