NSPredicate 帮助计算数组中的对象

发布于 2024-09-30 02:15:12 字数 841 浏览 1 评论 0原文

我有一个包含字典的数组,我想根据每个字典中的“名称”字段在表视图中显示值。假设名称以“a”开头;那么“b”和“c”位于不同的部分。部分的数量对应于词典的“名称”字段。如何获取行数?如果有人知道请给出解决方案。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //---get the letter in each section; e.g., A, B, C, etc.---
    NSString *alphabet = [subscribeIndexArray objectAtIndex:section];

    //---get all states beginning with the letter---

    //NSMutableArray *states =[[NSMutableArray alloc]init];
    NSArray *states;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];

    for(NSDictionary *dict in subscribeViewArray1)
    {
       states =[[dict valueForKey:@"name"] filteredArrayUsingPredicate:predicate];
    }

    //---return the number of states beginning with the letter---
    return [states count];
}

I have an array containing dictionaries, and I want to show the values in a table view according to the "name" field in each dictionary. Suppose the name starts with "a"; then "b" and "c" are in different sections. The number of sections corresponds to the "name" field of the dictionaries. How do I get the number of rows? If any one knows, please give the solution.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //---get the letter in each section; e.g., A, B, C, etc.---
    NSString *alphabet = [subscribeIndexArray objectAtIndex:section];

    //---get all states beginning with the letter---

    //NSMutableArray *states =[[NSMutableArray alloc]init];
    NSArray *states;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];

    for(NSDictionary *dict in subscribeViewArray1)
    {
       states =[[dict valueForKey:@"name"] filteredArrayUsingPredicate:predicate];
    }

    //---return the number of states beginning with the letter---
    return [states count];
}

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

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

发布评论

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

评论(1

差↓一点笑了 2024-10-07 02:15:12

您需要将状态名称放入数组中,以便使用谓词过滤它们。

试试这个:

...
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSMutableArray *matchingStatesArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in subscribeViewArray1) {
       //get the state names in an array
    [matchingStatesArray addObject:[dict objectForKey:@"name"]];
    }
    [matchingStatesArray filterUsingPredicate:predicate];

    return [[matchingStatesArray autorelease] count];
}

You need to have the state names in an array in order to filter them with a predicate.

Try this:

...
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSMutableArray *matchingStatesArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in subscribeViewArray1) {
       //get the state names in an array
    [matchingStatesArray addObject:[dict objectForKey:@"name"]];
    }
    [matchingStatesArray filterUsingPredicate:predicate];

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