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 Protocols in Swift Creating Flexible Objects Using Protocols Protocol Inheritance

William Squires
PRO
William Squires
Pro Student 2,817 Points

Correct answer marked as incorrect

"Protocols in Swift" : Challenge task 2 of 2

For the next step, create a struct named Dog that conforms to Pet (See the attached code)

This gives the "Bummer" response.

However, if I put this in an Xcode 8 playground (iOS), it does compile, and I can even check the results with: let fido = Dog(numberOfLegs: 4, cuddlyName: "Fido") // <- lldb_expr_30.Dog let fidosLegs = fido.numberOfLegs // <- 4 let fidosName = fido.cuddlyName // <- "Fido"

let aDog: Pet = fido // <- lldb_expr_30.Dog let aDogsLegs = aDog.numberOfLegs // <- 4 let aDogsName = aDog.cuddlyName // <- "Fido"

This clearly conforms to 'Pet' as I can access the 'numberOfLegs' (inherited from the Animal protocol), and the 'cuddlyName' (from Pet protocol) properties on the 'aDog' reference which is of type 'Pet'

It should not have said, "Bummer".

Please correct this so I can continue.

protocols.swift
protocol Animal {
  var numberOfLegs: Int { get }
}

protocol Pet : Animal
{
  var cuddlyName: String { get }
}

struct Dog : Pet
{
  var numberOfLegs: Int
  var cuddlyName: String
}

1 Answer

Hi William,

Your solution worked by simply removing the whitespace between Dog and the colon

struct Dog: Pet
{
  var numberOfLegs: Int
  var cuddlyName: String
}

Treehouse's IDE seems to be very picky sometimes!

All the best,

Mitch

Luc Bernardin
Luc Bernardin
3,726 Points

Probably a good idea to remove the whitespace after Pet as well.

Regards