Game Development Reference
In-Depth Information
enum DaysOfTheWeek: Int {
case Sunday = 0, Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday
func name() -> String {
switch self {
case .Sunday:
return "Sunday"
case .Monday:
return "Monday"
case .Tuesday:
return "Tuesday"
case .Wednesday:
return "Wednesday"
case .Thursday:
return "Thursday"
case .Friday:
return "Friday"
case .Saturday:
return "Saturday"
default:
return String(self.rawValue)
}
}
}
var today = DaysOfTheWeek.Friday
println("Today is \(today.name()).")
In the new version of the enumeration, you add a new method called
name()
that returns
a
String
representation of each member. It does this using a simple
case
statement that
tests
self
, the current instance of an
enum
, and returns its matching
String
. Notice
the test in the
case
statement. You are using a dot (
.
) followed by the member. You can
do this because you already know the value of
self
is a
DaysOfTheWeek
enumera-
tion. If you want to see the new changes in action, add the new code to
main.swift
and
try running it again.
Protocols