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

Erman Sahin Tatar
Erman Sahin Tatar
3,223 Points

Protocols in swift, Make sure Dog object conforms to PetType Error

it gives the error, Help me which part I misunderstood the topic I cant figure out

protocol AnimalType {
  var numberOfLegs: Int { get }
}

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

struct Dog : PetType {
  var numberOfLegs: Int
  var cuddlyName: String 

  init(numberOfLegs: Int, cuddlyName: String){
    self.numberOfLegs = numberOfLegs
    self.cuddlyName = cuddlyName

  }

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

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Erman Sahin Tatar,

Your protocol requires that the variable stored properties numberOfLegs and cuddlyName are readable, so you need to provide a way for them to be retrieved using get. You don't need an initializer, the memberwise initializer is provided automatically for structures.

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: String {
    get {
       return cuddlyName
    }
  }
}

Hope this helps!

Nathan Tallack
Nathan Tallack
22,159 Points

Note no space between the Dog and the colon on the struct declaration line for Dog. :)