case语句中未解决的标识符错误

时间:2016-08-31 20:55:07

标签: swift compiler-errors

此代码在perimeter()函数中导致“未解析的标识符”错误:

enum ShapeDimensions {
  case Square(Double)                                             // Associated value defined by length of one side
  case Rectangle(width: Double, height: Double)                   // Associate value defined by width and height
  case Point                                                      // No Associated value
  case RightTriangle(baseLength: Double, sideHeight: Double)      // Associated value defined by base length and height

  func area() -> Double {
    switch self {
      case let .Square(side):
        return side * side
      case let .Rectangle(width: w, height: h):
        return w * h
      case .Point:
        return 0.0
      case .RightTriangle(baseLength: l, sideHeight: h):
        return 0.5 * l * h
    }
  }

  func perimeter() -> Double {
    switch self {
      case let .Square(side):
        return 4 * side
      case let .Rectangle(width: w, height: h):
        return 2 * w + 2 * h
      case .Point:
        return 0.0
      case .RightTriangle(baseLength: l, sideHeight: h):
        return sqrt(l * l + h * h) + l + h
    }
  }

}

var squareShape = ShapeDimensions.Square(10.0)
var rectShape = ShapeDimensions.Rectangle(width: 5.0, height: 10.0)
var pointShape = ShapeDimensions.Point
var rightTriangleShape = ShapeDimensions.RightTriangle(baseLength: 5.0, sideHeight: 10.0)

print("Square's area = \(squareShape.area())")
print("Retangle's area  = \(rectShape.area())")
print("Point's area  = \(pointShape.area())")
print("Right Triangle's area = \(rightTriangleShape.area())")

print("Square's perimeter = \(squareShape.perimeter())")
print("Retangle's perimeter  = \(rectShape.perimeter())")
print("Point's perimeter  = \(pointShape.perimeter())")
print("Right Triangle's perimeter  = \(rightTriangleShape.perimeter())")

我收到了这些错误:

14-Enumerations.playground:221:29: error: use of unresolved identifier 'h'  
        return sqrt(l * l + h * h) + l + h  
                            ^   
14-Enumerations.playground:221:33: error: use of unresolved identifier 'h'  
        return sqrt(l * l + h * h) + l + h  
                                ^   
14-Enumerations.playground:221:38: error: use of unresolved identifier 'l'  
        return sqrt(l * l + h * h) + l + h  
                                     ^    
14-Enumerations.playground:221:42: error: use of unresolved identifier 'h'  
        return sqrt(l * l + h * h) + l + h 

代码有什么问题?

1 个答案:

答案 0 :(得分:0)

只需在每个案例之前添加let,你就应该好了。

enum ShapeDimensions {
  case Square(Double)                                             // Associated value defined by length of one side
  case Rectangle(width: Double, height: Double)                   // Associate value defined by width and height
  case Point                                                      // No Associated value
  case RightTriangle(baseLength: Double, sideHeight: Double)      // Associated value defined by base length and height

  func area() -> Double {
    switch self {
      case let .Square(side):
        return side * side
      case let .Rectangle(width: w, height: h):
        return w * h
      case let .Point:
        return 0.0
      case let .RightTriangle(baseLength: l, sideHeight: h):
        return 0.5 * l * h
    }
  }

  func perimeter() -> Double {
    switch self {
      case let .Square(side):
        return 4 * side
      case let .Rectangle(width: w, height: h):
        return 2 * w + 2 * h
      case let.Point:
        return 0.0
      case let .RightTriangle(baseLength: l, sideHeight: h):
        return sqrt(l * l + h * h) + l + h
    }
  }

}

var squareShape = ShapeDimensions.Square(10.0)
var rectShape = ShapeDimensions.Rectangle(width: 5.0, height: 10.0)
var pointShape = ShapeDimensions.Point
var rightTriangleShape = ShapeDimensions.RightTriangle(baseLength: 5.0, sideHeight: 10.0)

print("Square's area = \(squareShape.area())")
print("Retangle's area  = \(rectShape.area())")
print("Point's area  = \(pointShape.area())")
print("Right Triangle's area = \(rightTriangleShape.area())")

print("Square's perimeter = \(squareShape.perimeter())")
print("Retangle's perimeter  = \(rectShape.perimeter())")
print("Point's perimeter  = \(pointShape.perimeter())")
print("Right Triangle's perimeter  = \(rightTriangleShape.perimeter())")