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 Object-Oriented Swift Complex Data Structures Custom Initializers

Michael Bau
Michael Bau
3,394 Points

Help with init challenge!

Hi Community,

sigh I am stuck once more with a code challenge!

I can not figure out how to treat the 'description: String' constant. Are you supposed to add an unique initializer method here, or what? I constantly get the error message that a ‘self’ is missing regarding the ‘description’ leading me to believe that that is indeed the case, but I might, of course, be mistaken.

Any help here would be much appreciated!

Best regards, Michael

structs.swift
struct RGBColor {
  let red: Double
  let green: Double
  let blue: Double
  let alpha: Double

  let description: String


init(red: Double, green: Double, blue: Double, aplha: Double) {
  self.red = red
  self.green = green
  self.blue = blue
  self.alpha = yellow

}
  // Add your code below


}

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're so close here, that I feel like it'd be better at this point to just give you some hints. First, and foremost, you have a small error. You have self.alpha = yellow. But yellow was not passed in.... alpha was. Keep in mind that in the digital world the primary colors are red, blue and green. The "alpha" value indicates transparency of the color and nothing else.

  • self.alpha = alpha
  • You have all the building blocks now to build that string.
  • the string is comprised of a few labels as shown in the example
  • the string also contains the values of red, blue, green and alpha
  • self.description should equal the string that you've put together
  • no additional init or any other method is needed here

Hope this helps, but let me know if you're still stuck! :sparkles:

Michael Bau
Michael Bau
3,394 Points

Hi Jennifer,

Thanks a lot for your response!

I have tried the below code, but get the message: 'Make sure the string you're assigning to the description property matches the example in the instructions'.

I can't figure out what is missing as compared to the example provided in the code challenge, or if I am even on the right track here.

struct RGBColor {
    let red: Double
    let green: Double
    let blue: Double
    let alpha: Double

    let description: String


    init(red: Double, green: Double, blue: Double, alpha:Double) {

        self.red = red
        self.green = green
        self.blue = blue
        self.alpha = alpha

        self.description = "\(red), 86.0, \(green), 191.0, \(blue), 131.0, \(alpha), 1.0"

    } 

}

Is it the above 'self.description´ string interpolation the right approach?

Thanks and best regards, Michael

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Close, but not quite. Part of the problem here is that you've gotten the example numbers that are passed in upon creation of the RGBColor hard-coded in your string. The example says that they instantiate a new RGBColor with the values 86.0, 191.0, 131.0, 1.0. But we want those numbers to be changeable. I might want to create an entirely different color with the values 23.0, 88.0, 110.0, and 0.5. But if I were to do that right now with your code, this is what the description would hold: "23.0, 86.0, 88.0, 191.0, 110.0, 0.5, 1.0" which is obviously, not what we're looking for. Take a look at the line you need:

self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"

If I use this, with the values I just mentioned in my example above, the string stored would be "red: 23.0, blue: 86.0, green: 88.0, alpha: 0.5".

Hope this helps! :sparkles:

Michael Bau
Michael Bau
3,394 Points

Hi Jennifer,

Thanks again and thanks for the explanation. It works now!

Best regards, Michael