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

Ivan Cadena
Ivan Cadena
2,949 Points

Structs

Im not sure what I am doing wrong. I keep rewatching the videos and cant seem to figure it out

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){
 self.red = 86.0
        self.green = 191.0
        self.blue = 131.0
        self.alpha = 1.0
        self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"

}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Ivan,

You're on the right track, but there are a couple things going wrong here:

  1. The initializer (init) is outside of the struct's closing brace. The initializer is part of the struct, so it needs to be inside. Just move all of the init's code up into the struct.
  2. The challenge never asked you to hard-code any values. The values given in the Task are just examples of what it would want for output. Additionally, you don't usually hard-code values into an initializer. The values will come from the parameters passed in when an instance is created. So self.red = 86.0 will result in a Bummer. To initialize the values coming in, you will need self.red = red and so on for the rest of the variables.

Give it another go with these in mind. I'm sure you'll get it now. :) :dizzy:

Ivan Cadena
Ivan Cadena
2,949 Points

Thank you so much for you help :)