Getting Values from fetched Core Data
Thanks to the wonderful people on this forum, I have overcome most of my Core Data woes.
However one persists, and I'm certain it is a simple fix.
I have a recipe app that parses an XML doc on load and puts the data in Core Data.
Then I search that Core Data for particular recipes, ingredients, etc.
Everything is working with one exception... I cannot do anything with the data I retrieve.
For example, I search the core data for "eggplant" and get this at the end of the process:
"<RecipeData: 0x6112a40> (entity: RecipeData; id: 0x6113880 <x-coredata:///RecipeData/tCDE9A0EE-DA3F-4BD0-AEF8-3C038586991D4> ; data: {\n ingredients = \"Eggplant|Cheese|Tomatoes|\";\n name = \"Eggplant Parm\";\n time = 40;\n})"
How do I get the info out of there? I tried looping through, but tha开发者_运维技巧t causes the app to crash:
for (NSString* key in selectedRecipe) {
id value = [selectedRecipe objectForKey:key];
NSLog(@"IN LOOP: %@", value);
}
Any suggestions?
Thank you for your time.
You should already have a RecipeData class defined from your model, with synthesized properties already set. Include it in the file and then you an address the fetched results like this:
for (RecipeData *recipeData in yourFetchedResults) {
NSLog(@"The name is %@", recipeData.name);
NSLog(@"The ingredients are %@", recipeData.ingredients);
}
Etc. Obviously you'd use the actual name of your fetched results in place of yourFetchedResults.
If you don't already have the RecipeData class defined, it's easy to generate it automatically:
Click once on the RecipeData entity in your .xcdatamodel to select it.
Right/Control-click on a folder or group in your project where you'd like the class to be created and choose New File.
From the templates offered choose Managed Object Class and click on Next.
Check the boxes next to the entities you'd like to have classes generated for and click OK.
Poof, you've got the full class, properties already synthesized.
With Core Data, if you have a defined "Recipe" class created from the model, you should be able to read in a property/attribute like any other property:
someRecipe.ingredients = "Eggplant";
Your above code will only work if selectedRecipe is a dictionary. Other than that we'd need to see your implementation :)
I found this a good start:
http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html
精彩评论