Enumerations and Structures
enum 列舉
enum Rank: Int {
case ace = 1
case two, three, four, five, six, seven, eight, nine, ten
//會自己給rawValue two = 2, three = 3, ...
//Use the rawValue property to access the raw value of an enumeration case.
case jack, queen, king
func simpleDescription() -> String {
switch self {
case .ace:
return "aceA"
case .jack:
return "jackJ"
case .queen:
return "queenQ"
case .king:
return "kingK"
default:
return String(self.rawValue)
}
}
}
let ace = Rank.ace
//ace
let aceRawValue = ace.rawValue
//1
if let convertedRank = Rank(rawValue: 3) {
let threeDescription = convertedRank.simpleDescription()
//3
}
可縮寫為
ace.simpleDescription();
//aceA
0 意見:
張貼留言