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

Ben Thompson
Ben Thompson
1,430 Points

How do I know if I'm using the Memberwise initializer method?

I have typed up what I believe to be the correct code, but the message it gives me to tell me I'm wrong is to "Make sure you're not using the default Memberwise Initializer method. I'm not sure how to know if I am or not.

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


}

1 Answer

Thomas Dobson
Thomas Dobson
7,511 Points

Hi Ben,

Your spot on minus one thing. Remove description as part of your init inputs. You don't need a description input. Well done sir! Also set your initializers to themselves. You will initialize values for these when called.

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


}

Using your struct:

let tv = RGBColor(red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0)
tv.description
Ben Thompson
Ben Thompson
1,430 Points

Thank you! That was it! I was about to go crazy trying to figure that out.

Thomas Dobson
Thomas Dobson
7,511 Points

Hey Ben, made one more change I think you should see! Oversight when I first looked at it.