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 2.0 Complex Data Structures Custom Initializers

Ryan Maneo
Ryan Maneo
4,342 Points

I don't understand...

Can someone please try to explain the question in greater detail? I am having trouble understanding exactly what he wants me to do...

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

    let description: String

    // Add your code below
    init(red: Double, green: Double, blue: Double, alpha: Double) {
      red = 86.0
      green = 191.0
      blue = 131.0
      alpha = 1.0


}
}

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Ryan,

The challenge is asking you to create an initializer for RGBColor. Here's what the most initializer would look like if you wrote it out (note, you don't have to write this out for Structs - it's done behind the scenes for you):

youDontNeedToDoThis.swift
init(red: Double, green: Double, blue: Double, alpha: Double, description: String) {
      self.red = red
      self.green = green
      self.blue = blue
      self.alpha = alpha
      self.description = description
}

So this is done for free and we don't need to do it. But also, in this instance, we don't want to pass in a description - we want that to be generated on its own, based on the other properties. So we just have to change the description initialization, so it matches what the challenge is asking for:

solution.swift
init(red: Double, green: Double, blue: Double, alpha: Double) {
      self.red = red
      self.green = green
      self.blue = blue
      self.alpha = alpha
      self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}

Make sense? Let me know if you have other questions.

Ryan Maneo
Ryan Maneo
4,342 Points

Hello, I hope you still read this... I am struggling a lot with programming. That wasn't at all made clear. I don't know why but my brain doesn't connect the dots and do things automatically without instruction. Is this just how it is always with programming? The unknown? Constant trial and error? Tons of number crunching and complicated algorithmic problems to do simple tasks? > . <

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Ryan,

Every beginner programmer goes through the "Desert of Despair" as they're learning. Programming is hard! The best advice I can give you is to not rush the process. If you're having a hard time with a concept, go back and watch the video here again. Google the problem and read some blog posts / StackOverflow posts / watch youtube videos, etc. You'll get through it! It definitely becomes easier to grasp new concepts as you gain more experience.

I highly recommend reading this blog post about the "Desert of Despair": http://www.vikingcodeschool.com/posts/why-learning-to-code-is-so-damn-hard

Keep Coding!

-Greg