Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App with Swift 2.0 Improving Our User Interface Random Colors

William Forbes
William Forbes
21,469 Points

Random Colors

Regarding this video it appears that there was a fair amount of repetition in creating the colors array and I was surprised to see it handled in this way. I thought what was going to happen was to generate a random color. Does anyone know a library/method available in swift that would allow for easy generation of a "random" double that could be called as a method on a struct and be assigned to a var? This would allow us to plug in the variable as the numerator in each of the UIColor parameters and generate random colors each time the UIColor method is run?

From the research I have done I believe:

--In order to to do this, most research I have found states that we would have generate a random double between 0 and 1, and then multiply that by 255 using hardcore to ensure the numerator does not exceed 255. However, if any knows of a method in the XCode documentation that just allows us to set an upper bound of 255 and then generate a double between 0 and that upper bound I imagine would probably be easier but I have not seen anything like this out there.

--This would have to be done 3 times (one for each color in the RGB parameters), if using just one variable this would input the same number for all three colors each time effectively limiting our available colors to just 255 total colors, while this is more than enough it might be more fun to to use more!. By using 3 variables with 3 different values would would be able to utilize all potential 16 million-ish colors available with RGB.

Please let me know in the most frank terms if this would be a viable option, (I appreciate good criticism from experienced people),Just trying to think a little differently and maybe use less memory? It is entirely possible this method would not work I was just wondering about the potential viability of doing it this way, and also af this is how it would be done out in the "real" coding world so input from anyone with real/professional world experience would be greatly appreciated!

P.S. After writing this entire thing I just realized we would have to exclude a large portion of the 16 million colors as they may be too close to the color of the text on the screen making it difficult too read. If this method is possible and this is correct would anyone know of a way to avoid using these colors?

If anybody has anything to add or correct me all comments would be greatly appreciated as I am still fairly new to actually trying to build things using code!!

Thanks!!