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 trialStuart Robertson
Courses Plus Student 1,296 PointsProtocol Conformance Through Extensions
Hey! the error says: "Make sure you extend User and conform to PrettyPrintable"
What am I Doing Wrong! Typo?
protocol PrettyPrintable {
var prettyDescription: String { get }
}
struct User: PrettyPrintable {
let name: String
let ID: Int
var prettyDescription: String {
return "User name is \(name) and UserID is \(ID)"
}
}
// Enter your code below
1 Answer
Tobias Helmrich
31,603 PointsHey Stuart,
you were on the right track but in this case you want to add additional functionality to the existing User
struct by using an extension like you saw in the video before.
To do this you can use the extension
keyword in combination with the struct you want to add the functionality to followed by the protocol you want to conform to:
protocol PrettyPrintable {
var prettyDescription: String { get }
}
struct User {
let name: String
let ID: Int
}
// Enter your code below
extension User: PrettyPrintable {
var prettyDescription: String {
return "Name: \(name), ID: \(ID)"
}
}
I hope that helps, good luck! :)
Stuart Robertson
Courses Plus Student 1,296 PointsStuart Robertson
Courses Plus Student 1,296 PointsOh, that's very helpful. Thank You!! :)