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

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

let statement confusion

I am confused on the use of let descriptiion= I think we are suppose to write this one with out a function but I have not done so before. Where do we use the let description = String at

structs.swift
    struct Paint
    {
        let red: Double
        let green: Double
        let blue:  Double
        let alpha: Double


        // 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

        }


        func Palete( description: String) -> String
        {


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

            return description
        }
    }

let color = Paint(red: 11.0, green: 22.0, blue: 33.0, alpha: 44.0)

color.Palete(description: <#T##Paint#>)

2 Answers

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

I would first add a stored property for the string description to your Struct.

        let red: Double
        let green: Double
        let blue:  Double
        let alpha: Double

        // You need this stored property in your struct.
        let description: String

You want to initialize, or assign the description string a value, so you would do this in the init portion of the the struct.

init(red: Double, green: Double, blue: Double, alpha: Double)
        {
            self.red = red
            self.green = green
            self.blue = blue
            self.alpha = alpha

            // When we initialize a new object of type Paint, we give its description property this value.
            self.description = "red: \(red),  green: \(green), blue: \(blue), alpha: \(alpha)"
        }

Last call the method on your instance of Paint. Which will return your string.

    color.description()
Ingo Ngoyama
Ingo Ngoyama
4,882 Points

Whoops the editor on treehouse did not like it . It must want a string conversion of the color description.

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

Hello Chris. The editor for some reason says that the string for description doesn't match the example.

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

Thank you very much I see now to just treat the struct like a completely independent function. But the () on the description call is not needed.