将Object添加到NSMutableArray,它是NSDictionary的一个元素

时间:2021-06-26 13:49:42

In the code below, I declare an NSDictionary and fill it with empty NSMutableArrays designated by numeric keys:

在下面的代码中,我声明了一个NSDictionary并用数字键指定的空NSMutableArrays填充它:

NSDictionary *result = [[NSDictionary alloc] init];
for(float i = 0; i < 120; i += 0.25){
    [result setValue:[[NSMutableArray alloc] init] forKey:[NSString stringWithFormat:@"%.2f", i]];
}

Now if I have an object I want to add to one of the arrays and the key of the array I want to add it to (for example if I want to add a string to the array with key 4.25), how do I add the object to that array?

现在,如果我有一个对象,我想添加到其中一个数组和数组的键,我想添加它(例如,如果我想用键4.25向数组中添加一个字符串),我该如何添加对象到那个数组?

1 个解决方案

#1


0  

[result[@"4.25"] addObject: @"aString"];

Note that your code won't work as written. You need to create your dictionary as mutable if you are going to add key/value pairs to it after it's created.

请注意,您的代码将无法按写入方式工作。如果要在创建后添加键/值对,则需要将字典创建为可变字典。

#1


0  

[result[@"4.25"] addObject: @"aString"];

Note that your code won't work as written. You need to create your dictionary as mutable if you are going to add key/value pairs to it after it's created.

请注意,您的代码将无法按写入方式工作。如果要在创建后添加键/值对,则需要将字典创建为可变字典。