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

Sheng Wei
Sheng Wei
4,382 Points

[Help!] What does using my own custom initialiser for my init method mean?

This topic is doable, but the Bummer I keep getting is: Don't use the member wise initialisers given by swift, use your own custom initialisers. This wasn't really explained in the video so Im not sure what it means. Also, do I use string interpolation to represent the description? Much 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 = red
        self.green = green
        self.blue = blue
        self.alpha = alpha
        self.description = red, green, blue, alpha

    }

}

1 Answer

Wei Pang, what they want is for the description to be a String in the described format:

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

So if you create an RGBColor object you can ask for its description and get the String back saying what the value is for red, green, blue and alpha.

Note that you don't need self in front of description because there is no ambiguity as to which is the member variable.

Sheng Wei
Sheng Wei
4,382 Points

Thanks a lot jcorum! Your explanations are really clear :)