NSMutableDictionary作为值NSArray

时间:2014-09-29 08:08:07

标签: ios nsmutablearray nsmutabledictionary arc4random

嗨,我是目标C的新手,我很难进行数组索引

我需要创建一个包含5种颜色的NSMutableDictionary 每种颜色将包含随机生成的3个组件,alpha值将固定为1.0 这应该在for循环中。

到最后我需要

  1. key:first_col _________ 值:0.13,0.75,0.91,1.0
  2. key:second_col ______ 值:0.25,0.06,0.48,1.0
  3. key:third_col ________ 值:0.86,0.12,0.55,1.0
  4. key: _______________ 值:
  5. key: _______________ 值:
  6. 谢谢

2 个答案:

答案 0 :(得分:0)

喜欢这个

NSDictionary * colorsDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:
                             [UIColor colorWithRed:(160/255.0) green:(97/255.0) blue:(5/255.0) alpha:1.0],@"color1",
                             [UIColor colorWithRed:(260/255.0) green:(97/255.0) blue:(105/255.0) alpha:1.0],@"color2",
                             [UIColor colorWithRed:(106/255.0) green:(97/255.0) blue:(205/255.0) alpha:1.0],@"color2",
                             [UIColor colorWithRed:(10/255.0) green:(97/255.0) blue:(55/255.0) alpha:1.0],@"color3",
                             nil];

答案 1 :(得分:0)

试试这个答案..

  - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSMutableDictionary *dictonary;
        NSMutableArray *temp=[NSMutableArray array];
        for (int i=0; i<5; i++)
        {
            dictonary=[NSMutableDictionary dictionary];
            [dictonary setObject:[self getColor:i] forKey:[NSString stringWithFormat:@"color%d",i]];
            [temp addObject:dictonary];
        }
        NSLog(@"%@",temp);

    }
    -(UIColor *)getColor:(NSInteger)index
    {
        UIColor *color;
        color=[self returnColor:index ];
        return color;
    }
    -(UIColor *)returnColor:(int)passedIndex
    {


       CGFloat red = arc4random()%255;
       CGFloat green =arc4random()%255;
       CGFloat blue = arc4random()%255;

        UIColor *color;
        switch (passedIndex) {
            case 0:
                color=[UIColor colorWithRed:(red/255) green:(green/255) blue:(blue/255) alpha:1.0];
                break;
            case 1:
                color=[UIColor colorWithRed:(red/255) green:(green/255) blue:(blue/255) alpha:1.0];
                break;
            case 2:
                color=[UIColor colorWithRed:(red/255) green:(green/255) blue:(blue/255) alpha:1.0];
                break;
            case 3:
                color=[UIColor colorWithRed:(red/255) green:(green/255) blue:(blue/255) alpha:1.0];
                break;
            case 4:
                color=[UIColor colorWithRed:(red/255) green:(green/255) blue:(blue/255) alpha:1.0];
                break;
            case 5:
                color=[UIColor colorWithRed:(red/255) green:(green/255) blue:(blue/255) alpha:1.0];
                break;
            default:
                break;
        }
        return  color;
    }