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 trialPaul Zhang
464 PointsCould not pass last task in oop swift
It is the task of creating RGBColor init function. I tested my code and printed out the result. It matches the required outcome. But it keeps saying the value of description does not match. Can someone help me check? Thanks.
PS. I tried both with or without double quotes.
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
description = String(format:"red: %.1f, green: %.1f, blue: %.1f, alpha: %.1f", red, green, blue, alpha)
}
}
3 Answers
Ayso Lima Marques
5,122 Pointswrite that way:
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)")
}
}
Ayso Lima Marques
5,122 Pointsi'm not sure about it, cause i'm not that experienced with swift, but i think that u missed the "self." in the description initializer, and that your syntax it's more proper to objective - c, in swift it's more appropriate use string interpolation
self.description = ("red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)")
}
Since you don't use the "self." you're not referring to the property "description" that's outside the init
Paul Zhang
464 PointsNo, self.
is not the issue (tried and it failed as well).
It seems the tutorial is forcing us to use string interpolation and completely drop the back compatible ways.
But it would be hard to control the precision and I read from Swift Recipes: A Problem-Solution Approach:
"Note: When converting a Float to a string, you may see unpredictable results dueo to the binary representation of values. If this occurs, use NSString(format: "%.02f", str)
to control the precision."
Thanks for your reply and I will move on.
Ayso Lima Marques
5,122 Pointsyou're welcome, and yes, the treehouse's editor is limited, it aways expects us to solve the problem in a way equal or similar that is explained in the videos
Paul Zhang
464 PointsPaul Zhang
464 PointsHi Ayso, your change worked. But why?