enum Hotel{
case hotelName: "Raddisson"
case hotelCost: 200
case offer: 0.02
}
如何在枚举中分配多个类型?
答案 0 :(得分:0)
不幸的是,不可能有不同类型枚举的情况。枚举可以有一些raw type
,但所有情况下该类型都相同。以下是具有原始类型String
enum Hotel: String {
case hotelName: "Raddisson"
case hotelCost: "200"
case offer: "0.02"
}
答案 1 :(得分:0)
你应该在这里使用一个结构,例如:
struct Hotel {
let hotelName: String
let hotelCost: NSDecimalNumber
let offer: NSDecimalNumber
}
要使枚举具有多个值,您可以使用关联值执行此操作:
enum Hotel {
case standard(name: String, cost: NSDecimalNumber, offer: NSDecimalNumber)
case extended(name: String, cost: NSDecimalNumber, offer: NSDecimalNumber, discount: NSDecimalNumber)
}
如何从上面的枚举中提取值的示例是:
let hotel = Hotel.standard(name: "Hotel", cost: 50, offer: 10)
if case let Hotel.standard(name, cost, offer) = hotel {
print (name, cost, offer)
}
switch hotel {
case let .standard(name, cost, offer):
print (name, cost, offer)
case let .extended(name, cost, offer, discount):
print (name, cost, offer, discount)
}