类中的自我函数 参考类吧?
我想创建一个简单的函数,它可以完成大量尝试从核心数据获取数据的跑腿工作
,
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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题很简单,在编译第一个方法时,编译器不知道第二个方法。您应该在调用第二个方法之前定义它,如果不打算公开使用这些方法,则可以使用私有接口。
如果您打算让该方法是私有的,请在类实现之前放置类似的内容:
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: