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

Artem Kazanovskiy
Artem Kazanovskiy
1,124 Points

How can I convert this constants from Double to String and stick them to the right order? Cant understand...(

Hello everyone! Please, help me with this task because I`m dealing with that almost for 3 hours(

structs.swift
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) {

        self.red = "red"
        self.alpha = "alpha"
        self.blue = "blue"
        self.green = "green"


    }

}

2 Answers

Simon Di Giovanni
Simon Di Giovanni
8,429 Points

Hello Artem

I'll just let you know firstly - it's perfectly normal and ok to be frustrated when you don't understand something like this. I felt the same way when I first did this!

When you finally understand though, it will click quickly and you'll get it :-)

/* Firstly In your above code, all the constants are of type Double.

Yet you are trying to initialise them with assigned values that are of type String. e.g "red".

What you should be doing, is initialising them to accept the passed in value of the user of type Double.

Please take a look at my correct code below

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) {

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

So what I have done here, is initialised the struct to accept whatever value the user inputs for red, green, blue, alpha.

However I have assigned a String value to description, (just like you had done in your code, for all the constants!). That way, the user only inputs the values for red, green, blue, alpha.

Description, takes all the values the user inputs, and is combined into a string

I hope this makes sense. Please let me know if you have any questions

Regards

Simon

Artem Kazanovskiy
Artem Kazanovskiy
1,124 Points

Simon, thank you! You helped me so much!

Without you I would probably spent many hours in finding of right solution!