Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Intermediate Swift 2 Extensions and Protocols Extensions and Protocols

Alex Millius
Alex Millius
iOS Development Techdegree Student 5,468 Points

Incorrect answer

I think the response for this question is false

protocol SomeProtocol {
    func someMethod() -> Int

extension SomeProtocol {
    func someMethod() -> Int {
        return 1

struct SomeStruct: SomeProtocol {
    func someMethod() -> Int {
        return 2

let b: SomeProtocol = SomeStruct()

What is the value returned from the someMethod() call

The quizz says that the correct answer is 2. This doesn't sound right.

In any case, there is something wrong with this quiz. For the question:

 let a: SomeStruct = SomeStruct()

The answer given by the quiz is also 2

The two question can't have the same result

1 Answer

Greg Kaleka
Greg Kaleka
39,019 Points

Hi Alex,

The quiz answer is not incorrect; b.someMethod() will return 2 in this example. Try it out in a playground. The protocol extension provides a default implementation, but the definition of the struct overrides the protocol, and so that's the implementation you get.

As for the a.someMethod() and b.someMethod(), nothing is wrong there, except that it's the exact same question, which is probably a mistake. I assume something was meant to be changed between those two questions other than the name of the constant. Gabe Nadel - you may want to tweak that.

Cheers :beers: