嗨,我是目标C的新手,我很难进行数组索引
我需要创建一个包含5种颜色的NSMutableDictionary 每种颜色将包含随机生成的3个组件,alpha值将固定为1.0 这应该在for循环中。
到最后我需要
谢谢
答案 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;
}