gpt4 book ai didi

ios - 如何将 NSMutableDictionary setObject 的值赋值成为 indexPath.section 的值?

转载 作者:行者123 更新时间:2023-11-28 23:54:26 25 4
gpt4 key购买 nike

我有 NSMutableDictionary 并返回结果如下:-

NSMutableDictionary *result = [NSMutableDictionary new];
distinctNames = [_aryClippedCategoryList valueForKeyPath:@"@distinctUnionOfObjects.category_id"];
for (NSString *category_id in distinctNames) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category_id = %@", category_id];
NSArray *category_group = [_aryClippedCategoryList filteredArrayUsingPredicate:predicate];
[result setObject:category_group forKey:category_id];

NSLog(@"123 - %@", result);
NSLog(@"223 - %lu", (unsigned long)distinctNames.count);

在 NSLog 123 中,结果将如下所示:-

 17 =     (
{
"category_id" = 17;
"category_name" = "Apparel & Accessories";
"sub_category_id" = 17;
"sub_category_name" = "Fashion jewelry";
},
{
"category_id" = 17;
"category_name" = "Apparel & Accessories";
"sub_category_id" = 16;
"sub_category_name" = Wholesale;
}
);
20 = (
{
"category_id" = 20;
"category_name" = "Beauty & Personal Care";
"sub_category_id" = 288;
"sub_category_name" = "Pedicure & medicure";
},
{
"category_id" = 20;
"category_name" = "Beauty & Personal Care";
"sub_category_id" = 285;
"sub_category_name" = "Fragrances and perfumes";
}
);

在 NSLog 223 中,结果将如下所示:-

223 - 2

在这种情况下,如何在 CollectionView 中选择 category_group 作为部分值并在 CollectionView 单元格中显示每组项目(category_id、category_name、sub_category_id、sub_category_name)?

最佳答案

  • (不推荐)您应该使用 NSMutableArray 而不是 NSMutableDictionary。

    结构是

    Array<Dictionary<Array *> *> *

    代码是这样的

    NSMutableArray *groups = [NSMutableArray new];
    <# your code #>
    [groups addObject:@{
    @"category_id": category_id,
    @"infos": category_group
    }];

    然后

    NSString *groupID = groups[indexPath.section][@"category_id"]

    这是您需要的部分的组值。

    NSArray *infos = groups[indexPath.section][@"infos"];
    NSDictionary *info = infos[indexPath.item];

    这是对象dict元素值(value)

  • (推荐)或改变您的dataobject . (对象NSMutableArray<Object *> *)

    然后使用groups.count获取部分。

    groups[indexPath.section].categoryID

    这是您需要的 sections 的组值.

    group = groups[indexPath.section]
    object = group.infos[indexPath.item]

    这是 items 的对象值

关于ios - 如何将 NSMutableDictionary setObject 的值赋值成为 indexPath.section 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com