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

Fernando de Cerqueira Crelick
Fernando de Cerqueira Crelick
1,014 Points

Does anyone know the answer? I've coded it on my Xcode and it worked properly, still, doesn't work when I copy here

I have no clue where did I miss

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 = red
        self.green = green
        self.blue = blue
        self.alpha = alpha
        self.description = description
    }

    func assignValues() -> String{
        var description: String

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

        return description
    }
}

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

I don't think this works how the challenge is expecting, even in Xcode. In this challenge, you need to write an initializer (and only an initializer) that will set up all the data for a single instance of an RGBColor. You've done this really well with the red, green, blue, and alpha properties, but not so much for the description property. Currently, your initializer asks for a value for description, but it shouldn't do that. Instead, it should generate and assign one automatically based on what you pass in for the other properties

As a side note, your assignValues function doesn't do what I think you think it does. I think you're trying to use it to give a value to the self.description property, but what it's actually doing is creating a new variable also called description that's only visible inside of that function. It then generates exactly the string that the challenge asks for and returns it, but this function is never called, so ultimately nothing ever happens. (Hint: See if you can integrate this functionality into the initializer)

You're really close, and you have all the parts that's necessary, so I'm confident you're gonna figure it out from here!