Question

I've added the following to the AppDelegate in the Core Data Recipes example, but I don't get any printed result in the Console...any ideas?

#pragma mark -
#pragma mark TESTING RETRIEVAL OF RECIPE NAMES

- (NSArray *)allRecipes{
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"name" inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];

    NSArray *result = [[NSArray alloc] init];
    result = [self.managedObjectContext executeFetchRequest:request error:NULL];    

    NSManagedObject *firstObject = [result objectAtIndex:0];
    NSLog(@"firstObject.name: %@", [firstObject valueForKey:@"name"]);

    }

XCode gives me a warning after this method: "Control reaches end of non-void function"

I think that means I'm not getting any results...which is obvious, because nothing is printing.

Was it helpful?

Solution

Does "name" exactly match the class of the entity - NOT the attribute you want from the entity - in your .xcdatamodel file? Are you checking that in the app delegate the setup methods are completing without error? Have you checked for managedObjectContext != nil? Have you edited the .xcdatamodel file? If you have done so since running the program last you either need to delete the data file from the simulator's Documents directory for your application or you need to Add Model Version for your datamodel.

As gerry3 said, your first problem is that the function specifies you must return an array and you don't return anything.

Another problem is that you fetch attributes of class "name" but then you use "name" again as valueForKey. Do you really have an entity "name" with attribute "name"? More likely your entity is named something else and name is its attribute - in which case you need to alter the NSEntityDescription method entityForName: argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top