Methods 枚举方法调用解析为";“未使用的功能”;
我一直在斯威夫特玩Xcode游戏场。我知道Swift枚举比它们的Obj-C等价物强大得多。所以我想我应该创建一个包含颜色作为成员值的枚举,并向枚举中添加一个方法来获取颜色的十六进制值 但是,我得到一个错误--“表达式解析为未使用的函数”。我觉得这可能与方法接受成员值作为参数有关,但我可能错了。代码在下面。有人能启发我吗Methods 枚举方法调用解析为";“未使用的功能”;,methods,swift,enums,swift-playground,Methods,Swift,Enums,Swift Playground,我一直在斯威夫特玩Xcode游戏场。我知道Swift枚举比它们的Obj-C等价物强大得多。所以我想我应该创建一个包含颜色作为成员值的枚举,并向枚举中添加一个方法来获取颜色的十六进制值 但是,我得到一个错误--“表达式解析为未使用的函数”。我觉得这可能与方法接受成员值作为参数有关,但我可能错了。代码在下面。有人能启发我吗 enum Color { case Red,Blue,Yellow func hexValue (aColor: Color) -> String { //Find
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"
请阅读错误消息
let color = Color.hexValue(Color.Red)
您必须将返回值指定给某个对象,请阅读错误消息
let color = Color.hexValue(Color.Red)
您必须将返回值指定给某个对象将
static
添加到hexValue
的声明中,以创建一个可以从类型调用的类型方法,而无需实例:
enum Color {
case Red,Blue,Yellow
static 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) // "#FF0000"
或者,您可以通过将其设置为计算属性,使其变得更好:
将
static
添加到hexValue
的声明中,以创建一个类型方法,该方法可以在不使用实例的情况下从类型调用:
enum Color {
case Red,Blue,Yellow
static 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) // "#FF0000"
或者,您可以通过将其设置为计算属性,使其变得更好:
您必须将
hexValue
函数定义为静态函数,因为您不是从实例调用它
请注意,default
的大小写是不必要的,因为在switch语句中已经处理了所有可能的值
但是,swift枚举比这更强大。这就是我如何利用原始值实现它的方法:
enum Color : String {
case Red = "#FF0000"
case Blue = "#FFFF00"
case Yellow = "#0000FF"
static func hexValue(aColor: Color) -> String {
return aColor.toRaw()
}
}
并使用以下方法获取字符串表示形式:
Color.hexValue(Color.Red)
但是,hexValue
方法是多余的,因为您可以只使用:
Color.Red.toRaw()
您必须将
hexValue
函数定义为静态函数,因为您不是从实例调用它
请注意,default
的大小写是不必要的,因为在switch语句中已经处理了所有可能的值
但是,swift枚举比这更强大。这就是我如何利用原始值实现它的方法:
enum Color : String {
case Red = "#FF0000"
case Blue = "#FFFF00"
case Yellow = "#0000FF"
static func hexValue(aColor: Color) -> String {
return aColor.toRaw()
}
}
并使用以下方法获取字符串表示形式:
Color.hexValue(Color.Red)
但是,hexValue
方法是多余的,因为您可以只使用:
Color.Red.toRaw()
另一个选项是使用
Printable
协议并在枚举上实现description
computed属性,然后您可以使用字符串插值引用其值
enum Color: Printable {
case Red,Blue,Yellow
var description: String {
get {
switch self {
case .Red: return "#FF0000"
case .Yellow: return "#FFFF00"
case .Blue: return "#0000FF"
}
}
}
}
let myColor = "\(Color.Red)"
另一个选项是使用
Printable
协议并在枚举上实现description
computed属性,然后您可以使用字符串插值引用其值
enum Color: Printable {
case Red,Blue,Yellow
var description: String {
get {
switch self {
case .Red: return "#FF0000"
case .Yellow: return "#FFFF00"
case .Blue: return "#0000FF"
}
}
}
}
let myColor = "\(Color.Red)"
哇,我不知道你可以用这样的计算属性。(我知道你可以做Color.hexValue,但不能做Color.Red.hexValue)非常感谢!哇,我不知道你可以用这样的计算属性。(我知道你可以做Color.hexValue,但不能做Color.Red.hexValue)非常感谢!