gpt4 book ai didi

ios - 维护 NSMutableDictionary 元素的顺序

转载 作者:行者123 更新时间:2023-11-29 13:54:14 26 4
gpt4 key购买 nike

<分区>

我得到以下格式的字典:

[
{
"position": "1",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "2",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "1",
"name": "Jaen",
"group": "Teacher",
"Address": "Delhi"
}
]

基本上,我想围绕名为“group”的键对它们进行分组。我必须按以下格式创建字典以在我的 UI 中填充数据。

[
{
"Student": [
{
"position": "1",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "2",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
}
],
"Teacher": [
{
"position": "1",
"name": "Jaen",
"group": "Teacher",
"Address": "Delhi"
}
]
}
]

组的顺序应该保持不变,即学生应该在老师之前。但是当我打印 dictGroupField 时,它会根据顺序给出随机结果。

如何维护订单?

请在下面找到我使用的代码:

    @property (strong, nonatomic) NSMutableDictionary *dictGroupField;


-(void)loadFieldData {

_dictGroupField = [[NSMutableDictionary alloc] init];

NSMutableArray *arrayFields = [self getArrayFields];

for (NSDictionary *dictT in arrayFields) {
NSString *strGroup = [dict valueForKey:@"group"];

if ([_dictGroupField valueForKey:strGroup] == nil) {

NSMutableArray *arrGroup = [[NSMutableArray alloc] init];
[arrGroup addObject:dict];
[_dictGroupField setObject:arrGroup forKey:strGroup];
} else {
NSMutableArray *arrGroupExist = [_dictGroupField valueForKey:strGroup];
[arrGroupExist addObject:dict];
[_dictGroupField setObject:arrGroupExist forKey:strGroup];
}
}

}

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