Objective c 在Obj-C中随机选择一个项目
我使用以下方法将背景颜色更改为三种颜色之一:Objective c 在Obj-C中随机选择一个项目,objective-c,Objective C,我使用以下方法将背景颜色更改为三种颜色之一: - (void) setBackgroundOfView { // change the background color UIColor *feijoa = [UIColor colorWithRed:0.565 green:0.82 blue:0.478 alpha:1]; /*#90d17a*/ UIColor *turquoise = [UIColor colorWithRed:0.2 green:0.78 blue:
- (void) setBackgroundOfView {
// change the background color
UIColor *feijoa = [UIColor colorWithRed:0.565 green:0.82 blue:0.478 alpha:1]; /*#90d17a*/
UIColor *turquoise = [UIColor colorWithRed:0.2 green:0.78 blue:0.773 alpha:1]; /*#33c7c5*/
UIColor *lavendar = [UIColor colorWithRed:0.765 green:0.541 blue:0.898 alpha:1] /*#c38ae5*/
UIColor *randomColor = random.choose(feijoa, turquoise, lavendar) // in pseudocode
}
正确的随机方法是什么?选择(feijoa、绿松石、lavendar)?您可以将颜色存储到
NSArray
中,然后随机选择其中一种:
#include <stdlib.h>
- (void) setBackgroundOfView {
// change the background color
UIColor *feijoa = [UIColor colorWithRed:0.565 green:0.82 blue:0.478 alpha:1]; /*#90d17a*/
UIColor *turquoise = [UIColor colorWithRed:0.2 green:0.78 blue:0.773 alpha:1]; /*#33c7c5*/
UIColor *lavendar = [UIColor colorWithRed:0.765 green:0.541 blue:0.898 alpha:1] /*#c38ae5*/
NSArray *colors = @[feijoa, turquoise, lavendar];
int index = arc4random_uniform(colors.count);
UIColor *randomColor = colors[index];
}
#包括
-(无效)观点的倒退{
//更改背景色
UIColor*feijoa=[UIColor color with red:0.565 green:0.82 blue:0.478 alpha:1];/*#90d17a*/
UIColor*绿松石色=[UIColor颜色与红色:0.2绿色:0.78蓝色:0.773阿尔法:1];/*#33c7c5*/
UIColor*lavendar=[UIColor COLOR WITHRED:0.765绿色:0.541蓝色:0.898阿尔法:1]/*#c38ae5*/
NSArray*颜色=@[飞鸟、绿松石、拉文达尔];
int index=arc4random_uniform(colors.count);
UIColor*randomColor=颜色[索引];
}
您可以将颜色存储到NSArray
中,然后随机选择其中一种:
#include <stdlib.h>
- (void) setBackgroundOfView {
// change the background color
UIColor *feijoa = [UIColor colorWithRed:0.565 green:0.82 blue:0.478 alpha:1]; /*#90d17a*/
UIColor *turquoise = [UIColor colorWithRed:0.2 green:0.78 blue:0.773 alpha:1]; /*#33c7c5*/
UIColor *lavendar = [UIColor colorWithRed:0.765 green:0.541 blue:0.898 alpha:1] /*#c38ae5*/
NSArray *colors = @[feijoa, turquoise, lavendar];
int index = arc4random_uniform(colors.count);
UIColor *randomColor = colors[index];
}
#包括
-(无效)观点的倒退{
//更改背景色
UIColor*feijoa=[UIColor color with red:0.565 green:0.82 blue:0.478 alpha:1];/*#90d17a*/
UIColor*绿松石色=[UIColor颜色与红色:0.2绿色:0.78蓝色:0.773阿尔法:1];/*#33c7c5*/
UIColor*lavendar=[UIColor COLOR WITHRED:0.765绿色:0.541蓝色:0.898阿尔法:1]/*#c38ae5*/
NSArray*颜色=@[飞鸟、绿松石、拉文达尔];
int index=arc4random_uniform(colors.count);
UIColor*randomColor=颜色[索引];
}
我将设置三个阵列。红色、绿色、蓝色各一个。然后使用随机数生成器选择索引。然后插入arrayRed[index]、arrayGreen[index]、array[Blue]arrayBlue[index]
UIColor *feijoa = [UIColor colorWithRed:arrayRed[index) green:arrayGreen[Index] blue:arrayBlue[Index] alpha:1];
使用
arc4Random()
函数生成索引。此方法现在允许您轻松地向数组添加更多颜色或不同的颜色。我将设置三个数组。红色、绿色和蓝色各一个。然后使用随机数生成器拾取索引。然后插入arrayRed[index)、arrayGreen[index”、array[blue]arrayBlue[索引]
UIColor *feijoa = [UIColor colorWithRed:arrayRed[index) green:arrayGreen[Index] blue:arrayBlue[Index] alpha:1];
使用
arc4Random()
函数生成索引。此方法现在允许您轻松地向数组或不同的数组添加更多颜色。您有一组结果:
UIColor *feijoa = [UIColor colorWithRed:0.565 green:0.82 blue:0.478 alpha:1]; /*#90d17a*/
UIColor *turquoise = [UIColor colorWithRed:0.2 green:0.78 blue:0.773 alpha:1]; /*#33c7c5*/
UIColor *lavendar = [UIColor colorWithRed:0.765 green:0.541 blue:0.898 alpha:1] /*#c38ae5*/
您需要随机选择一个。将结果放入一个数组:
NSArray *choices = @[feijoa, turquoise, lavender];
然后选择一个随机索引:
int index = arc4random() % ([choices count]);
因此,选择[索引]是你的项目你有一套结果:
UIColor *feijoa = [UIColor colorWithRed:0.565 green:0.82 blue:0.478 alpha:1]; /*#90d17a*/
UIColor *turquoise = [UIColor colorWithRed:0.2 green:0.78 blue:0.773 alpha:1]; /*#33c7c5*/
UIColor *lavendar = [UIColor colorWithRed:0.765 green:0.541 blue:0.898 alpha:1] /*#c38ae5*/
您需要随机选择一个。将结果放入一个数组:
NSArray *choices = @[feijoa, turquoise, lavender];
然后选择一个随机索引:
int index = arc4random() % ([choices count]);
所以,选择[索引]您的项目是否
索引
、索引
和蓝色
都是相同的索引?代码中也有语法错误。索引
、索引
和蓝色
都是相同的索引吗?代码中也有语法错误。这与Gustavo的方法非常类似,但应该是arc4random()[choices count]
,否则最后一项将永远不会被选中。请注意,根据,arc4random_uniform()
建议优于arc4random()%。
(但是在这种情况下可能无关紧要)。这与Gustavo的方法非常相似,但应该是arc4random()%[choices count]
,否则将永远不会选择最后一项。请注意,根据,arc4random\u uniform()
建议优先于arc4random()%…
(但在这种情况下可能并不重要)。