ios - dictionaryWithObjectsAndKeys 内循环

标签 ios objective-c

尝试使用 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/

相关文章:

objective-c - 对 NSMutableSet 类型的属性使用保留与复制的语义是什么?

c++ - 通过 USB 将文件从 iOS 设备传输到 Windows PC

ios - 如何在 Objective-C 类别中使用内部方法?

ios - Swift:分段控件在 UITableView Cell 中的行为很奇怪

iPhone : How to use loop to generator a 256 color table in iphone?

iphone - 非 ARC(自动引用计数)和 ARC 用户的开源

ios - JSONObjectWithData 解析时错误的小数位

ios - iOS 网盘 : keeping user logged in

iphone - iOS模拟器认为文档目录是/var/root/Documents

ios - Objective-C中另一个线程中的计时器