FetchRequest 上的 If 语句

发布于 2024-11-03 20:01:14 字数 1097 浏览 0 评论 0原文

我该如何做到这一点,以便我只获取指标 == 1 的结果。(我将指标连接到另一个视图上的 UISwitch)。所以我想要一个标题表,但前提是指标为 1。

-(void)reload
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Title" ascending:YES]; 
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc]initWithKey:@"indicator" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil]; 
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release]; 
    [sortDescriptor release];

    NSError *error = nil; 
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    [self setList:mutableFetchResults]; 
    [mutableFetchResults release]; 
    [request release];
}

How do I make it so I only fetch results with the indicator == 1. (I have the indicator hooked up to a UISwitch on another view). so I want a table of titles, but only if the indicator is 1.

-(void)reload
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Title" ascending:YES]; 
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc]initWithKey:@"indicator" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil]; 
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release]; 
    [sortDescriptor release];

    NSError *error = nil; 
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    [self setList:mutableFetchResults]; 
    [mutableFetchResults release]; 
    [request release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

恰似旧人归 2024-11-10 20:01:14

向您的提取请求添加谓词:

[request setPredicate:[NSPredicate predicateWithFormat:@"indicator == 1"]];

这将导致仅返回设置了该谓词的事件。我还会从排序描述符中删除您的 sortDescriptor2 。当指标键都具有相同的值时,对它们进行排序是没有意义的。

Add a predicate to your fetch request:

[request setPredicate:[NSPredicate predicateWithFormat:@"indicator == 1"]];

That'll cause only the events that have it set to be returned. I'd also remove your sortDescriptor2 from the sort descriptors. No sense doing to the work to sort of the indicator key when they'll all have the same value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文