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

Marina Alenskaja
Marina Alenskaja
9,320 Points

I don't really understand what is wrong in my answer. I wrote out the init code just like in the video?

Regarding initializer code challenge in Object-Oriented Swift course: I don't really understand what is wrong in my answer. I wrote out the init code just like in the video and added the values with self. It says that i can't use the initializer the struct function makes automatically, but should write my own - which is what I have done as far as I can tell..

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)"
}
}

7 Answers

Stephan Porsche
Stephan Porsche
2,864 Points

Hey, I finally got it. Had a glitch some where. Your are also nearly there. All what's missing is the initializer for "description" like you already wrote it before.

Like this:

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)" }

Please note that it's not present in the init(....) term.

Corey F
PLUS
Corey F
Courses Plus Student 6,450 Points

Without fully giving you the answer.

A. Your Blue initializer method parameter is a capital. Make it lowercase as per camelCase. (blue)

B. The values of 86, 191, 131, 1 for color are "For example". They do not belong in anywhere your code. They are meant to just give you example numbers to use as arguments when creating instance of RGBColor in your playground to see if your code works.

e.g. outside your struct let color1 = RGBColor(red: 12.0, green: 86.0, blue: 131.0, alpha: 1.0)

Again this is not used for the challenge answer.

C. description shouldn't be a parameter. When we create an instance of RGBColor, we aren't going to specify a description as an argument... we want to put in arguments to our parameters of red, green, blue and alpha to create a description.

D. If the format of the description will be "red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0" ... then in your code above you are missing the commas and a whitespace after the "red:" .

My terminology might not 100% accurate, but I hope you can get the gist of what I am saying.

Derrold McPhee
Derrold McPhee
2,086 Points

This Challenge was very frustrating because it has nothing to do with actual coding, but the formatting of the description field. Make sure every space and comma is accounted for, else you won't get credit although it compiles just fine in Xcode.

Stephan Porsche
Stephan Porsche
2,864 Points

Hi Marina & Corey, I'm stuck there as well. I wrote nearly the same code like you but it wouldn't match the solution. I tried like every possible solution in XCodes' Playground and apparently they all worked there. :(

PS: It always says:

swift_lint.swift:17:5: error: return from initializer without initializing all stored properties } ^ swift_lint.swift:9:9: note: 'self.description' not initialized let description: String

But I'm sure I did.

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hey. Yeah, i sort of understand what I did wrong after Corey's answer but I can't get further than this:

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

I just really don't get what I'm supposed to do with the description!........ I think the way the challenge is formulated is sort of off, but maybe that's just me.

Marina Alenskaja
Marina Alenskaja
9,320 Points

Okay, so now I got SOMETHING right. At least it works, but the challenge says "the value of the description does not match with the directions specified"...

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: \(self.red) green: \(self.green) blue: \(self.blue) alpha: \(self.alpha)"
    }
}

var colorProject = RGBColor(red: 80.0, green: 108.0, blue: 39.4, alpha: 39.49)
colorProject.description
Marina Alenskaja
Marina Alenskaja
9,320 Points

i missed the commas.. you have to be kidding me! Well, now it works :) Thanks for the help!