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

Jason Ernsdorff
Jason Ernsdorff
3,112 Points

Says that the description string doesn't match what is expected in the challenge.

I don't understand what is wrong. When I create an instance of the struct with init method it says:

description "red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0" when inspecting it which is what is expected unless I am missing something obvious.

What is the problem? Is this code challenge not working?. Works fine in Xcode 9.2.

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

2 Answers

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

Hey Jason Ernsdorff,

You're almost there! The way your initializer is set up you're always only going to assign those specific values since any values provided as arguments to the initializer when you create an instance are not used anywhere.

You need to change the body of your init method to assign the relevant arguments passed in via the parameters to the stored properties.

Hope this helps

Jason Ernsdorff
Jason Ernsdorff
3,112 Points

Ok Pasan.

I hadn't thought about doing that. Here's what worked:

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

I guess my values will never be over-ridden if I don't pass in the values back into self. I get it now.

Thanks so much.

-Jason

Jason Ernsdorff
Jason Ernsdorff
3,112 Points

BTW: I've tried this with both the description listed in the init properties and without. I've also tried with and without self. Neither of these things worked.