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

Rémi Villien
Rémi Villien
4,717 Points

I don't know where I'm wrong :/

I also tried to create a function that return the string, i tried to not use custom value like self.red = red

On xcode i don't have any errors and it work, soo I'm really stuck :(

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, description: String){
    self.red = 56.0
    self.green = 120.0
    self.blue = 24.0
    self.alpha = 1.0
    self.description = "red: \(self.red), green: \(self.green), blue: \(self.blue), alpha: \(self.alpha)"
  }

}

1 Answer

Shade Wilson
Shade Wilson
9,002 Points

Hi Remi,

There are two main things wrong with your code. First, you don't need description: String as an argument in your init method. If you had this, this would mean you'd have to pass in a description yourself when creating an instance of RGBColor, which we don't want. We want to create the description variable on the fly.

Second, the RBG values given in the description of the challenge were merely meant to be values you could pass in when creating an instance of RGBColor. You shouldn't use them within the actual init method. Instead, you want to pass in the argument name for each variable. Otherwise, we'd have no need for an init method.

For instance, it should be:

self.red = red // don't give a specific value for red here because we want users to be able to pass in their own values

Lastly, for description you should pass in the arguments given to init instead of the self values. It may work fine this way, but it's better just to pass in the actual arguments. It's also less typing! Ex:

self.description = "red: \(red), "// etc...
Rémi Villien
Rémi Villien
4,717 Points

Thanks you so much :D !