-2

i've stored colors in a dictionary which is added to a nsmutable array.i want to retrieve values of the dictionary from another class based on the keys.thanks in advance

-(NSMutableArray *) GetAllColor 
{
    UIColor *aminocolor1 = [UIColor colorWithRed:.09 green:.56 blue:.30 alpha:.1];
    UIColor *aminocolor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:.1];

    NSDictionary *colordict = [NSDictionary dictionaryWithObjectsAndKeys:
                              aminocolor1, @"1",
                              aminocolor2, @"2",
                              nil];

    NSMutableArray *colors = [NSMutableArray arrayWithObjects:colordict, nil];
    return colors;
}
4

3 回答 3

0

您可以将该数组声明为全局数组,以便在其他类中使用该数组。

于 2013-09-13T08:49:02.573 回答
0

您可以只链接消息:

NSDictionary* colorDict = [colors objectAtIndex:0];
UIColor *aminocolor = [colorDict objectForKey:@"1"];

或在一行中相同:

UIColor *aminocolor = [[colors objectAtIndex:0] objectForKey:@"1"]; 
于 2013-09-13T07:55:32.107 回答
0

看到这段代码,我不禁要问,你为什么要加成NSDictionary一个NSArray?你为什么不直接退货NSDictionary

现在最简单的解决方案:

如果你想得到所有的NSDictionaryNSArray如果你有超过 1 个)

for(NSDictionary *dict in colors) {
   //do whatever you want with the dictionary (now it's the dict that has the collors)
   [dict objectForKey:@"1"] // this is how you get animcolor1.
}

如果您只想获取某个索引处的字典:

if([colors count] > 0 && [colors count] < yourIndex) { //be smart and perform validations ;)
     NSDictionary *dict =  [colors objectAtIndex:yourIndex];
    //do whatever you want with the dictionary (now it's the dict that has the collors)
    [dict objectForKey:@"1"] // this is how you get animcolor1.

}
于 2013-09-13T07:58:41.500 回答