我正在探索Swift并尝试编写一个函数,通过比较它们的原始值来比较两个Rank值。但我得到错误:缺少参数' rawValue'在电话卡().compareValue(card.Ace,card.King)
有什么想法吗?
enum card : Int {
case Ace = 1
case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
case King, Queen, Jack
func cardValue() -> String {
switch self {
case .Ace:
return "Ace"
case .King:
return "King"
case .Queen:
return "Queen"
case .Jack:
return "Jack"
default:
return String(self.rawValue)
}
}
func compareValue(val1: card, val2: card) -> String {
if val1.rawValue > val2.rawValue {
return "\(val1) is greater than \(val2)"
} else {
return "\(val1) is not greater than \(val2)"
}
}
}
card().compareValue(card.Ace, card.King) // error
答案 0 :(得分:2)
问题在于构造card()
对象,即调用的目标:
card(). // and so on
您正在尝试构建enum
而不指定其原始值。您可以通过提供它来修复它,例如
card(rawValue:0). // <<=== This is not a good fix!
但这不是一个好的解决方法。相反,您应该compareValue
static
或独立功能:
static func compareValue(val1: card, _ val2: card) -> String
// ^
... // Note the underscore ----------+
card.compareValue(card.Ace, card.King) // Works