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

iOS Protocols in Swift Creating Flexible Objects Using Protocols Protocol Inheritance

William Squires
PLUS
William Squires
Courses Plus 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