我想创建一个简单的函数来完成从核心数据中获取数据的大量工作
目前
NSArray * FetchResult = [self fetchEntities: entityForName sortKey: sortKey PredicateString:PredicateString];
我收到编译器警告说
- (NSArray *)fetchEntities: (NSString *) entityForName sortKey: (NSString *) sortKey PredicateString: (NSString *)PredicateString is not found.
但是谁在乎呢?我知道我应该调用[self class] fetchEntities而不是[self fetchEntities ...]但它是一个类函数。自我已经提到了课程。那有什么问题?
+(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];
}
答案 0 :(得分:1)
问题很简单,在编译第一个方法时,编译器不知道第二个方法。您应该在调用之前定义第二个方法,如果方法不会公开使用,可能使用私有接口。
如果您要将方法设为私有方式,请在类实现之前加上类似的内容:
@interface <MyClassName> ()
+ (NSArray *)fetchEntities: (NSString *) entityForName sortKey: (NSString *) sortKey PredicateString: (NSString *)PredicateString;
@end