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 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
    }
}

}