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

Jonathan Law
seal-mask
.a{fill-rule:evenodd;}techdegree
Jonathan Law
iOS Development Techdegree Student 6,824 Points

Can't solve Challenge Task 2 of 2 in Protocols Inheritance...

My question is about Task 2 of 2 but the "get help" button is not working for that section. I have pasted my code into the field for Task 1. Task 2 is to create a "Dog" object that conforms to PetType... although there seems to be multiple errors in my code. Very confused -- any help would be appreciated.

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

protocol PetType: AnimalType {
  var numberOfLegs: Int { get }
  var cuddlyName: String { get }
  }

struct Dog: PetType {

  var numberOfLegs: Int {
      get {
      return numberOfLegs
        }
    }

  var cuddlyName: Int {
      get {
      return cuddlyName
        }
    }

}

3 Answers

Uriel Kreszes
Uriel Kreszes
3,759 Points

To make task 2 is very simple... You only have to create the struct with the requirements that the protocol PetType ask, that's to have a variable named cuddlyName and because PetType is a protocol inheRitance of the protocol AnimalType you alse need to put the requirement of the protocol AnimalType that is that the struct need to have a variable named numberOfLegs.

I hope you understand it. If not ask and i will try to answer.

Here is how it has to be at the end of task 2:

protocol AnimalType {
  var numberOfLegs: Int { get }
}

protocol PetType: AnimalType {
  var cuddlyName: String { get }
}

struct Dog: PetType {
  var numberOfLegs: Int
  var cuddlyName: String
}
Connor Crawford
Connor Crawford
6,405 Points

Hey Jonathon, you are making it too complicated for yourself. To get it to conform to the protocol you must enter in some data for the variable stored properties. Try this, it should work.

struct Dog: PetType { var numberOfLegs = 4 var cuddlyName = "Spot" }

Hi Jonathan,

Two fixes are needed to your code: 1- add self. before variable name when returning it 2- cuddlyName variable should be of type String not Int

here you go modified code:

struct Dog: PetType {

var numberOfLegs: Int {
    get {
        return self.numberOfLegs
    }
}

var cuddlyName: String {
    get {
        return self.cuddlyName
    }
}

}