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

Robert Deegan
Robert Deegan
2,848 Points

Init Method not working

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, description: String) {
    self.red = 1.0
    self.green = 2.0
    self.blue = 3.0
    self.alpha = 1.0

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

}

Error says "don't use the member wise init method???

2 Answers

Hello.

The challenge doesn't ask you to hardcode any values. Nor to pass any values to the description property at initialisation.

Try reading the instructions more carefully and aim to understand them before coding. That is gonna make a world of difference. :)

This might help :

 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)"
    } 
Robert Deegan
Robert Deegan
2,848 Points

Thanks. I had actually gone that route at first. When it didn't work, I tried adding the values. I think the problem was in how I did the description string, using "self".

Kurt Daisley
Kurt Daisley
4,228 Points

Is there any way for the Code Challenges to give more specific hints when the code isn't what it's looking for? Here was my attempt at the init challenge:

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

let description: [String: Double]

init (red: Double, green: Double, blue: Double, alpha: Double, description: [String:Double]) {

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

}

}

This worked fine in Xcode, but wasn't accepted by the challenge. It really kinda stumps me when it keeps repeating the same sentence over and over.

I'd assume that the challenge's programmer would need to think of every possible answer a student might provide, and that is virtually impossible.

You changed the code provided in the challenge and that is a big no-no!

Leave the provided code as it is, and use string interpolation to complete the challenge.