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

Esther Newman
PLUS
Esther Newman
Courses Plus Student 5,015 Points

initialiser quiz

i don't think i fully understand what am supposed to do with the quiz, the working below is what i "think" am supposed to do but it says am wrong so am obviously missing something. Any help in clarifying would be greatly appreciated.

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 = 86.0
        self.green = 191.0
        self.blue = 131.0
        self.alpha = 1.0

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

2 Answers

Anusha Singh
PLUS
Anusha Singh
Courses Plus Student 22,106 Points

Hey Esther, you were almost there, except that you had to use the parameters for initializing the values in the init function. Also, you don't really need to add description for the parameter. Small typo- the string which you had typed was also incorrect, so try copying the string which they have assigned you and then filling in the values, because the treehouse editor is a little picky. Here's the code I used to pass the challenge

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)"
    }
}
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Esther,

You've got the right general idea but you don't want description to be a parameter to the initialiser. If you look at how you're setting the value of self.description you're not using the description value passed into the initialiser, you're just constructing it from the various colour values passed in.

Moreover, because every parameter in the initialiser is required, your struct cannot be initialised without giving a description value. This will cause the code to not compile because the Treehouse system is trying to initialise an instance with only the colour values being passed in.

There is another small error where you assign the text description to self.description. If you look closely at what the task is asking for, there should be spaces between each colour's name and its value:

the description property should look like this:

"red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0"

However, if you look at what your code generates, it will look like this: red:86.0, green:191.0, blue:131.0, alpha:1.0

Hope that helps

Cheers

Alex