user6780597 user6780597 - 3 months ago 27
Swift Question

Unresolved identifier errors in case statement

This code is causing "unresolved identifier" errors inside the

perimeter()
function:

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


I'm getting these errors:


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


What is wrong with the code?

Answer

Just add the let before each case and you should be good to go.

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