【问题标题】:Enum method call resolves to to "unused function"枚举方法调用解析为“未使用的函数”
【发布时间】: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


    【解决方案1】:

    另一种选择是使用Printable 协议并在您的枚举上实现description 计算属性,然后您可以使用字符串插值来引用它的值。

    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)"
    

    【讨论】:

      【解决方案2】:

      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"
      

      或者您可以通过将其设为计算属性来使其更好:

      enum Color {
      
          case Red,Blue,Yellow
      
          var hexValue: String {
              get {
                  switch self {
                      case .Red:     return "#FF0000"
                      case .Yellow:  return "#FFFF00"
                      case .Blue:    return "#0000FF"
                  }
              }
          }
      }
      
      Color.Red.hexValue    // "#FF0000"
      

      【讨论】:

      • 哇——我没有意识到你可以使用这样的计算属性。 (我知道你可以做 Color.hexValue 但不能做 Color.Red.hexValue)非常感谢!
      【解决方案3】:

      您必须将hexValue 函数定义为静态,因为您不是从实例调用它。

      请注意,default 的情况是不必要的,因为所有可能的值都已在您的 switch 语句中处理。

      然而,快速枚举比这更强大。这就是我将如何实现它,利用原始值:

      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()
      

      【讨论】:

        【解决方案4】:

        只需阅读错误消息!

            let color = Color.hexValue(Color.Red)
        

        您必须将返回值分配给某物

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-01-25
          • 2020-11-06
          • 2019-12-03
          • 1970-01-01
          • 1970-01-01
          • 2019-03-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多