I’ve written a code to select a random color from an NSArray, but how can I make it so that the same color doesn’t get selected 2 times in a row? Any help would be greatly appreciated.
+ (NSArray *)colors {
static NSArray *colors = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{ colors = @[
//green
[UIColor colorWithRed:88.0/255.0 green:195.0/255.0 blue:98.0/255.0 alpha:1],
//yellow
[UIColor colorWithRed:246.0/255.0 green:194.0/255.0 blue:48.0/255.0 alpha:1],
//orange
[UIColor colorWithRed:236.0/255.0 green:135.0/255.0 blue:40.0/255.0 alpha:1],
//red
[UIColor colorWithRed:226.0/255.0 green:51.0/255.0 blue:40.0/255.0 alpha:1],
//blue
[UIColor colorWithRed:59.0/255.0 green:156.0/255.0 blue:218.0/255.0 alpha:1],
//violet
[UIColor colorWithRed:138.0/255.0 green:70.0/255.0 blue:165.0/255.0 alpha:1],
];
});
return colours;
}
_selectedColor = [colors objectAtIndex: arc4random() % [colors count]];