gpt4 book ai didi

iphone - 如何将对象数组添加到以另一个数组为键的 nsmutabledictionary

转载 作者:太空狗 更新时间:2023-10-30 03:59:40 25 4
gpt4 key购买 nike

for(Attribute* attribute in appDelegate.attributeArray) {
attribute = [appDelegate.attributeArray objectAtIndex:z];
attri = attribute.zName;
int y = 0;
for(Row* r in appDelegate.elementsArray) {
r = [appDelegate.elementsArray objectAtIndex:y];
NSString *ele = r.language;
if([attri isEqualToString:ele]) {
NSLog(@"=================+++++++++++++++++%@ %@",attri, r.user);
[aaa insertObject:r atIndex:y]; //here i am adding the value to array
[dict setObject:aaa forKey:attri]; //here i am adding the array to dictionary
}
y++;
}
z++;
NSLog(@"============$$$$$$$$$$$$$$$$$$$$$$$++++++++++ %@",dict);
}

键在一个数组中,值在另一个数组中,值数组是对象格式。

我需要为单个键存储多个对象。 attributeArray 具有键值,elementsArray 具有对象。例如 attributeArray 可能有值

 " English, French, German..."

并且 elementsArray 可能具有对象值

"<Row: 0x4b29d40>, <Row: 0x4b497a0>, <Row: 0x4e38940>, <Row: 0x4b2a070>, <Row: 0x4b29ab0>, <Row: 0x4b178a0> "

在第一个值中,我需要存储两个对象,对于第二个键,我需要存储 3 个对象,对于第三个键,我需要在字典中存储最后两个对象。

最佳答案

为了 super 简化,您可以使用以下代码:

NSArray *keys = ...;
NSArray *values = ...;

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects: values forKeys: keys];

希望这对您有所帮助。

更新:

要在字典中为单个键存储多个值,只需使用 NSArray/NSMutableArray 作为您的对象:

NSArray *keys = ...;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

for( id theKey in keys)
{
NSMutableArray *item = [NSMutableArray array];
[item addObject: ...];
[item addObject: ...];
...

[dict setObject: item forKey: theKey];
}

如果一开始不知道key的所有值,需要一个一个的相加,可以使用下面的方法:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

for( /*some cycling condition here */)
{
id keyToUse = ...;
id valueToAdd = ...;

id foundArray = [dict objectForKey: keyToUse];
if ( nil == foundArray )
{
foundArray = [NSMutableArray array];
[dict setObject: foundArray forKey: keyToUse];
}

[foundArray addObject: valueToAdd];
}

关于iphone - 如何将对象数组添加到以另一个数组为键的 nsmutabledictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238266/

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