我已经将颜色存储在字典中,该字典被添加到nsmutablearray.i想要根据keys.thanks提前从另一个类中检索字典的值
-(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;
}
答案 0 :(得分:0)
您可以将消息链接起来:
NSDictionary* colorDict = [colors objectAtIndex:0];
UIColor *aminocolor = [colorDict objectForKey:@"1"];
或者在一行中相同:
UIColor *aminocolor = [[colors objectAtIndex:0] objectForKey:@"1"];
答案 1 :(得分:0)
看到这段代码,我不得不问,为什么要将NSDictionary
添加到NSArray
?你为什么不回归NSDictionary
?
现在最简单的解决方案:
如果您想从NSDictionary
获取所有NSArray
(如果您有超过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.
}
答案 2 :(得分:0)
您可以将该数组声明为全局,以便在其他类中使用该数组。