Skip to content

Instantly share code, notes, and snippets.

@SteveTrewick
Created June 18, 2014 12:00
Show Gist options
  • Save SteveTrewick/422f723db03f86e30d5d to your computer and use it in GitHub Desktop.
Save SteveTrewick/422f723db03f86e30d5d to your computer and use it in GitHub Desktop.
class Horse {
var name:String?
var beenThroughDesert:Bool = false
init(){}
init(horsename:String?, desert:Bool) {
if let n = horsename {
name = n
}
beenThroughDesert = desert
}
}
func identifyHorse( horse: Horse? ) -> String {
var horseid:String
switch horse {
case let h where h?.name? == "Ed":
horseid = "It's Mr Ed, of course."
case let h where h?.name? == "Champion":
horseid = "It's Champion the wonder horse!"
case let h where (h?.name? == nil) && (h?.beenThroughDesert == false):
horseid = "It's a mysterious but untested horse."
case let h where (h?.name? == nil) && (h?.beenThroughDesert == true):
horseid = "It's the horse with no name!"
case let h where h? == nil:
horseid = "There is no horse"
default:
horseid = "I don't know this horse."
}
return horseid
}
var mred = Horse(horsename: "Ed", desert: false)
var champion = Horse(horsename: "Champion", desert: false)
var hwnn = Horse(horsename: nil, desert: false)
var myst = Horse(horsename: nil, desert: false)
identifyHorse(mred) // It's Mr Ed, of course.
identifyHorse(champion) // It's Champion the Wonder Horse!
identifyHorse(myst) // It's a mysterious but untested horse
identifyHorse(hwnn) // It's the horse with no name!
var nohorse:Horse?
identifyHorse(nohorse) // There is no horse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment