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

Kristof Kocsis
Kristof Kocsis
15,455 Points

I don't understand how to create the decription!

So far I have tried

  1. creating it by continuing the line where it is declared.
  2. creating a method for it which return a string that I called description (this worked in xcode)
  3. including it in the init method None of theme seemed to work, so how am I supposed to make that description work.
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 = red
        self.green = green
        self.blue = blue
        self.alpha = alpha
    }


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


}

1 Answer

Simon Di Giovanni
Simon Di Giovanni
8,429 Points

Hi Kristof

There are a few things wrong with what you've written.

Most importantly, you have not initialised 'description'. 'description' is a stored property just like any of the other values and must be initialised.

Please take a look at my example below, which was accepted

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 = red
        self.green = green
        self.blue = blue
        self.alpha = alpha
        self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
    }


}

let instance = RGBColor(red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0)

instance.description

Firstly, take a look at this line

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

I'm initialising 'description' by assigning it a String. This way, it doesn't require a user inputted String to be initialised, it just uses what I've entered. I've put (red) as it's going to accept the user inputted value for 'red', as we initialised 'red' to accept whatever the user inputs into there. Same for green, blue & alpha.

Next thing that you need to look at. You had assigned a second constant, in the same struct, named 'description'. Within the same scope you can't name two constants the same thing, as you had done.

Lastly -

let instance = RGBColor(red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0)

instance.description

These two lines are for you to take a look at in your Playground. I've created an instance of 'RBGColor' and assigned it some values.

I've then accessed the stored property of 'description' to see what it returns. Take a look in your Playground to see what's returned.

I hope this helps. Please let me know if you have any questions or if anything I've written doesn't make sense.

Regards

Simon