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 trialDavid kidd
1,684 PointsIm unable to create a set of colour variables and then store them in an array.
I have come back to this exercise to experiment with the method briefly mentioned in this video. Rather than creating the array with all the full UIColour value information inside i wanted to create a set of variables and then store the colour variables inside the array. Its a method that is mentioned and briefly shown in the playground part of this video.
However when i do this i can create the variables with no issue but when i add them to an array i get an error i don't understand
ColourWheel.type does not have a member named testCol
testCol is the name of one of my colour variables (for experimentation)
The code works fine inside a playground but does not function inside my ColourWheel Struct.
Thanks
3 Answers
Sebastian Röder
13,878 PointsI had the same problem and solved it by moving all the initialization code into the init()
function of the struct:
import Foundation
import UIKit
struct ColorWheel {
let colors: [UIColor]
init() {
let teal = UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0)
let yellow = UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0)
let red = UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0)
let orange = UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0)
let darkGray = UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0)
let purple = UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0)
let green = UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0)
colors = [teal, yellow, red, orange, darkGray, purple, green]
}
}
These individual color variables are not visible outside the init()
function and the ColorWheel
struct has only one member: colors
. When you do not put the color constants inside the init()
function, they are not local variables but members of the struct (e.g. you can access ColorWheel.red
from outside the struct). When I understand the error message correctly, you cannot set values of member variables by using other member variables during initialization, but I have to read up the details in the The Swift Programming Language iBook (it provides a lot of details about structs).
My motivation to rewrite the code was to get rid of the comments and use variable names instead. As I am using let
there should not be any performance implications, but I'd like to get feedback about that.
David kidd
1,684 PointsAh i have not come across the int() function yet. Ive spent the last hour or so playing about in playgrounds with it though and i think it does solve the issue i had. Ill have to read up on them more though to make sure i really understand whats going on here.
Thanks
David kidd
1,684 PointsAh i have not come across the int() function yet. Ive spent the last hour or so playing about in playgrounds with it though and i think it does solve the issue i had. Ill have to read up on them more though to make sure i really understand whats going on here.
Thanks
David kidd
1,684 PointsDavid kidd
1,684 PointsI have now realised that the method works if i place the variable creation outside of the boundaries of the ColourWheel Struct. Can anyone explain why this is, i feel like I've missed something.
Thanks again!