gpt4 book ai didi

ios - dictionaryWithObjectsAndKeys 内循环

转载 作者:行者123 更新时间:2023-11-29 01:34:46 25 4
gpt4 key购买 nike

尝试使用 dictionaryWithObjectsAndKeys 向字典添加值,我想使用 for 循环添加对象,但我找不到正确的出路,请帮忙,使用 for 循环仅显示最后一个值,其他值不出现.看起来这些值是覆盖的并且没有被附加。

没有for循环的正常代码

toListDictUnSorted = [NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects:[NSNumber numberWithInt:316], @"USD 50000", @"English", nil], @"USA",
[NSArray arrayWithObjects:[NSNumber numberWithInt:194], @"USD 12000", @"Portugese", nil], @"Brazil",
[NSArray arrayWithObjects:[NSNumber numberWithInt:1210], @"USD 1592", @"Hindi", nil], @"India",
[NSArray arrayWithObjects:[NSNumber numberWithInt:1353], @"USD 6075", @"Chinese", nil], @"China",
[NSArray arrayWithObjects:[NSNumber numberWithInt:143], @"USD 14037", @"Russian", nil], @"Russia",
[NSArray arrayWithObjects:[NSNumber numberWithInt:59], @"USD 33115]", @"Italian", nil], @"Italy",
[NSArray arrayWithObjects:[NSNumber numberWithInt:63], @"USD 38591", @"English", nil], @"Great Britain",
[NSArray arrayWithObjects:[NSNumber numberWithInt:29], @"USD 25000", @"Arabic", nil], @"Saudi Arabia",
nil]; */

for循环代码

for (int i = 0; i < [_toData count]; i++)
{

toListDictUnSorted = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects:[NSNumber numberWithInt:meters], [[_toData objectAtIndex:i] valueForKey:@"name"], nil], [[_toData objectAtIndex:i] valueForKey:@"name"] , nil];



}

最佳答案

你应该使用 NSMutableDictionary 而不是 NSDictionary

编辑:

你想要这个:

NSMutableDictionary *toListDictUnSorted = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [_toData count]; i++)
{
[toListDictUnSorted setObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:meters], [[_toData objectAtIndex:i] valueForKey:@"name"], nil] forKey:[[_toData objectAtIndex:i] valueForKey:@"name"]];
}

关于ios - dictionaryWithObjectsAndKeys 内循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33090275/

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