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

Emre Havan
Emre Havan
8,569 Points

Init function problem for a Struct

I was trying to solve the challenge for Structs in ios.

I am new to programming, and I believe that I've written the codes for this task as it is supposed to be written.

I just cant move forward, and I don't want to jump ahead before Im done with this.

Do you have any idea why I am getting an error when I check my work?

IM COPYING THE TASK DESCRIPTION BELOW:

---In the editor, I’ve declared a struct named RGBColor that models a color object in the RGB space.

Your task is to write a custom initializer method for the object. Using the initializer assign values to the first four properties. Using the values assigned to those properties create a value for the description property that is a string representation of the color object.

For example, given the values 86.0 for red, 191.0 for green, 131.0 for blue and 1.0 for alpha, each of the stored properties should hold these values and the description property should look like this:

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

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


    }




}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing terrific. Really, you are. There are many who get stumped on this one and not even close to the reason you're stumped. Yours is a tiny little omission. Your string in the description is supposed to have commas in between the colors. Take another look at the example string and see what I mean.

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

When I insert the commas in the right places in your code, it passes with flying colors! :sparkles:

Emre Havan
Emre Havan
8,569 Points

Ah thank you so much! I forgot some commas :D it was driving me crazy and Im finally done with it! Thanks!!

Michael Williams
Michael Williams
Courses Plus Student 8,059 Points

Like the OP, I ran into a problem. However, I had assigned number values to each of the properties, i.e.:

self.red = 86.0 self.green = 191.0 self.blue = 131.0 self.alpha = 1.0

I did this because I was reading Apple's language guide and it showed examples where you give properties specific values in the init method. So why doesn't this work for this exercise.

Secondly, why isn't description a parameter in the init method? Like so:

init(red: Double, green: Double, blue: Double, alpha: Double, description: String) {}

Also, I was tracking with the struct stuff, albeit a little slowly, but this init stuff really confused the heck out of me–even after rewatching the video a bunch of times. Will it start to make sense later on?

Emre Havan
Emre Havan
8,569 Points

Hi Michael,

I am also new to the programming and Swift syntax but as I know, "description" is not a parameter in the init method. Because, we dont want any user to assign it to any value or string, While we do self.x = x for others, we dont do it for description, since we want it to return a value contains the parameters entered by the user, so we assign it differently.

Lets say there is going to be an "x" value in a "class". We want "x" to be, lets say "10" for every instances of this class. So while writing the init of this class, we dont ask it from a user as a parameter, but we write it down like "self.x = 10" instead. So now this x value is going to have 10 value for every instances created from this class in the future.

I hope this helps.

Also, this init was also confusing me, but I kept practicing and now I feel like, I have a better understanding of it :)