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 trialErman Sahin Tatar
3,223 PointsProtocols 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
}
}
protocol AnimalType {
var numberOfLegs: Int { get }
}
2 Answers
Steven Deutsch
21,046 PointsHey 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!
Erman Sahin Tatar
3,223 PointsYes, thanks a lot :)
Nathan Tallack
22,160 PointsNathan Tallack
22,160 PointsNote no space between the Dog and the colon on the struct declaration line for Dog. :)
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 PointsNathan Tallack, (cry) how did you see it?