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

Claudio Candido Domingos Lopes Junior
Claudio Candido Domingos Lopes Junior
880 Points

Hi, I've tried all I could but got stuck. Please help me with the answer to this quiz

init(red: Double, green: Double, blue: Double, alpha: Double, description: String) { self.red = 20.8 self.green = 22.3 self.blue = 43.1 self.alpha = 22.9 self.description = "red:(red), green:(green), blue:(blue), alpha:(alpha)" }

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

    let description: String

    // Add your code below
}

3 Answers

Hi Claudio,

In your code, you are currently setting the self values to constant Doubles. The struct should be able to return any set of Double values, not just 20.8, 22.3 etc.

To make the code flexible, you need to assign the stored color properties (self.red, self. green etc) to the color values passed in by the initializer method (red, green etc).

The proper code looks like this.

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)"
} 
}
James Knight
James Knight
4,680 Points

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, description: String) {
    self.red = 20.8
    self.green = 22.3
    self.blue = 43.1
    self.alpha = 22.9
    self.description = "red:(red), green:(green), blue:(blue), alpha:(alpha)"
}

}

// I hope this helps, James

Claudio Candido Domingos Lopes Junior
Claudio Candido Domingos Lopes Junior
880 Points

Hi James,

Thanks for your prompt response. I've tried your suggestion and something very similar as shown below and unfortunately both of them didn't work.

init(red: Double, green: Double, blue: Double, alpha: Double, description: String) { self.red = 20.8 self.green = 22.3 self.blue = 43.1 self.alpha = 22.9 self.description = "red: (red), green: (green), blue: (blue), alpha: (alpha)" }

James Knight
James Knight
4,680 Points

import UIKit

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, description: String) { self.red = 20.8 self.green = 22.3 self.blue = 43.1 self.alpha = 22.9 self.description = "red:(red), green:(green), blue:(blue), alpha:(alpha)" } }

//Seems strange no errors are produced when compiled