I want to create simple function that do a lot of the leg work of trying to get fetch data from core data
At
NSArray * FetchResult = [self fetchEntities: entityForName sortKey: sortKey PredicateString:PredicateString];
I got a compiler warning saying that
- (NSArray *)fetchEntities: (NSString *) entityForName sortKey: (NSString *) sortKey PredicateString: (NSString *)PredicateString is not found.
But who care? I know I should have called [self class] fetchEntities rather than [self fetchEntities…] But it’s a class function. Self already refer to the class. So what’s the problem?
+(NSManagedObject *)FirstfetchEntity: (NSString *) entityForName sortKey: (NSString *) sortKey PredicateString: (NSString *)PredicateString
{
NSArray * FetchResult = [self fetchEntities: entityForName sortKey: sortKey PredicateString:PredicateString];
if ([FetchResult count]==0)
{
return nil;
}
else
{
return [FetchResult objectAtIndex:0]; //return the First
}
}
+(NSArray *)fetchEntities: (NSString *) entityForName sortKey: (NSString *) sortKey PredicateString: (NSString *)PredicateString
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init]autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityForName inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:sortKey ascending:NO]autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, nil]autorelease];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate *predicate = [NSPredicate predicateWithFormat:PredicateString];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
// Handle the error
}
return fetchedObjects;
[pool release];
}
The problem is simply that at the time the first method is compiled, the compiler doesn’t know about the second method. You should define the second method before calling it, perhaps with a private interface if the methods isn’t going to be used publicly.
Put something like this before your class implementation if you’re going to have the method be private: