类中的自我函数 参考类吧?

发布于 2024-11-06 00:27:57 字数 2024 浏览 1 评论 0原文

我想创建一个简单的函数,它可以完成大量尝试从核心数据获取数据的跑腿工作

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-11-13 00:27:57

问题很简单,在编译第一个方法时,编译器不知道第二个方法。您应该在调用第二个方法之前定义它,如果不打算公开使用这些方法,则可以使用私有接口。

如果您打算让该方法是私有的,请在类实现之前放置类似的内容:

@interface <MyClassName> ()
   + (NSArray *)fetchEntities: (NSString *) entityForName sortKey: (NSString *) sortKey PredicateString: (NSString *)PredicateString;
@end

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:

@interface <MyClassName> ()
   + (NSArray *)fetchEntities: (NSString *) entityForName sortKey: (NSString *) sortKey PredicateString: (NSString *)PredicateString;
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文