【发布时间】:2014-11-30 03:44:53
【问题描述】:
我一直在用 Xcode Playgrounds 玩弄 Swift。我知道 Swift 枚举比它们的 Obj-C 等价物强大得多。所以我想我会创建一个包含颜色作为成员值的枚举,并向枚举添加一个方法来获取颜色的十六进制值。
但是,我收到一个错误 - “表达式解析为未使用的函数”。我有一种感觉,这可能与让该方法接受成员值作为参数有关,但我可能错了。代码如下。有人可以启发我吗?
enum Color {
case Red,Blue,Yellow
func hexValue (aColor: Color) -> String { //Find hex value of a Color
switch aColor {
case .Red:
return "#FF0000"
case .Yellow:
return "#FFFF00"
case .Blue:
return "#0000FF"
default:
return "???????"
}
}
}
Color.hexValue(Color.Red) //Error: "Expression resolves to an unused function"
【问题讨论】:
标签: methods swift enums swift-playground