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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
philip williams
5,991 Pointsstruggling with extensions a lot, can anyone help?
I don't understand whats going. Any help will be greatly appreciated.
protocol Math {
func square(value: Double) -> Double
}
// Enter your code below
extension Math {
func squareValue() -> Double {
return ("The square of \(value) is \(value * value)")
}
}
2 Answers
![Steven Deutsch](https://uploads.teamtreehouse.com/production/profile-photos/612592/micro_Unknown.jpg)
Steven Deutsch
21,046 PointsHey philip williams,
Nehemiah Reese was kind enough to give you a hint, but let me see if I can help clear up your understanding of extensions for you.
An extension is just a way of adding additional functionality to a type. This could be a native Swift type, a type in one of the iOS Frameworks, or even a custom type of our own.
In Swift, protocol's are first class citizens. They are fully fledged types of their own. This means we can use an extension on them like we would any other type.
If you recall, protocols are blueprints in a sense - blueprints of what properties and methods our data types will contain. They are not an actual implementation of these properties and methods. They just define what they will look like.
In order for a data type to conform to a protocol, it has to implement all the properties and methods that are defined in that protocol. This also means that we have to use the same naming conventions for the method names, computed property names, and the methods need to have to same function signatures.
So what we want to do here is extend the Math protocol and give it an actual implementation instead of previously just having the blueprint defined in the original protocol.
protocol Math {
func square(value: Double) -> Double
}
// Enter your code below
// Take note how my implementation of this method matches the one defined in the protocol
// Same name, parameter names, and signature
// The only thing new here is we are adding the function body
extension Math {
func square(value: Double) -> Double {
return value * value
}
}
Good Luck
philip williams
5,991 PointsThanks Steven and Nehemiah. Really appreciate the help.
Nehemiah Reese
17,692 PointsNehemiah Reese
17,692 PointsHint: your returning a Double in the function declaration but your returning a String in the implementation