Methods

You can use a protocol to declare a method requirement. In this case, the adopting implementation must use the methods listed in the protocol in order to conform to it. Here is an example of this situation with your new Animizable protocol:

enum Food:String {    case Meat = "meat"    case Veggies = "veggies"    case Other = "something else"}protocol Animizable {    var type:String { get }    func eat(quantityInPounds:Double, what:Food)}class Animal:Animizable {    var type = ""    func eat(quantityInPounds:Double, what:Food){        println("I am \(type) and I am eating \(quantityInPounds) pounds of           \(what.toRaw()).")    }}var human = Animal()human.type = "human"human.eat(2,what: .Meat)

Here you ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.