简体   繁体   中英

Add Object to an NSMutableArray that is an element of an NSDictionary

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

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?

[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM