enum TapSwitch {
case on
case off
}
extension TapSwitch: ItemDiscribable {
var description: String {
switch self {
case .on: return "current is on"
case .off: return "current is off"
}
}
}
extension TapSwitch: Togglable {
mutating func toggle() {
switch self {
case .on:
self = .off
case .off:
self = .on
}
}
}
enum PaymentType: String {
case cheque(issuer:String)
case creditCard(number:String)
}
// ใช้ case argument และต้องการเป็น raw type String
// โปรแกรมจะไม่สามารถรันได้
enum PaymentType {
case cheque(issuer:String)
case creditCard(number:String)
var description: String {
switch self {
case let .cheque(issuer): return "Cheque issued by \(issuer)"
case let .creditCard(number): return "Credit card number \(number)"
}
}
}
extension PaymentType: RawRepresentable {
typealias RawValue = String
init?(rawValue: RawValue) {
switch rawValue {
case "Cheque": self = .cheque(issuer: "")
case "CreditCard": self = .creditCard(number: "")
default: return nil
}
}
var rawValue: String {
return description
}
}