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 trialMichael Williams
1,498 PointsCustom 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
Jason Anders
Treehouse Moderator 145,860 PointsCould you link to the actual challenge this is referring to. It will make it easier for us to help you troubleshoot the problem.
Michael Williams
1,498 PointsLink in post! Thanks so much for that. (https://teamtreehouse.com/library/objectoriented-swift-20/complex-data-structures/custom-initializers)
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHey 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! :)
Michael Williams
1,498 PointsBut 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.
Michael Williams
1,498 PointsMichael Williams
1,498 PointsSorry the post messes up my code.... It is just a struct called RGBColor with properties red, green, blue, alpha, and description.