Skip to content

Instantly share code, notes, and snippets.

@konrad1977
Last active April 14, 2016 19:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save konrad1977/d468f9d493c33eecefac to your computer and use it in GitHub Desktop.
Save konrad1977/d468f9d493c33eecefac to your computer and use it in GitHub Desktop.
Person with Protocol
protocol AgeClasificationProtocol {
var age: Int { get }
func agetype() -> String
}
class Person {
let firstname: String
let lastname: String
var age: Int
init(firstname: String, lastname: String) {
self.firstname = firstname
self.lastname = lastname
self.age = 10
}
}
extension Person : AgeClasificationProtocol {
func fullname() -> String {
return firstname + " " + lastname
}
func agetype() -> String {
switch age {
case 0...2:
return "Baby"
case 2...12:
return "Child"
case 13...19:
return "Teenager"
case let x where x > 65:
return "Elderly"
default:
return "Normal"
}
}
}
@NatashaTheRobot
Copy link

I'd recommend having the extension conform to the AgeClasificationProtocol, since that's where the code is. https://gist.github.com/NatashaTheRobot/2f0b83b483a49917ea04

@konrad1977
Copy link
Author

Thank you Natasha. I missed that. I also updated firstName and lastName to constants.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment