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

Michael Williams
Michael Williams
1,498 Points

Custom Initializer Swift 2.0 CODE WONT WORK

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 = red
        self.green = green
        self.blue = blue
        self.alpha = alpha

        self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"

    }

}

This should be the custom initializer it is asking for. It compiles fine and fulfills the criteria but it still says "Bummer, use a custom initializer instead of Swifts default initializer."

thanks :(

EDIT:!! Here is the link to the challenge https://teamtreehouse.com/library/objectoriented-swift-20/complex-data-structures/custom-initializers

Michael Williams
Michael Williams
1,498 Points

Sorry the post messes up my code.... It is just a struct called RGBColor with properties red, green, blue, alpha, and description.

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Could you link to the actual challenge this is referring to. It will make it easier for us to help you troubleshoot the problem. :dizzy:

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Michael,

You are so close, but the error you received gives the hint as to what's wrong. The struct has 5 constants, and therefore, Swift will generate a constructor with all four constants. The challenge wants you to create a custom initializer that only 'initializes' 4 constants (red, green, blue, alpha)... it doesn't want you to initialize the description.

So, really, it's a very easy fix. Just remove the description from your 'custom' initializer.

init(red: Double, green: Double, blue: Double, alpha: Double) {

Keep Coding! :) :dizzy:

Michael Williams
Michael Williams
1,498 Points

But in a playground, it says not every property is initialized! Don't you need to initialize every property?

Thank you for your responses!

EDIT:!! I fully understand now!! THANKS SO MUCH! I have description property in the initializer, just not on the description of the initializer.