Chapter 14, Silver Challenge Solution

import Cocoa

enum ShapeDimensions {
    // point has no associated value - it is dimensionless
    case point
    
    // square's associated value is the length of one side
    case square(side: Double)
    
    // rectangle's associated value defines its width and height
    case rectangle(width: Double, height: Double)
    
    // triangle's associated value defines its width and height
    case triangle(width: Double, height: Double)
    
    func area() -> Double {
        switch self {
        case .point:
            return 0
            
        case let .square(side: side):
            return side * side
            
        case let .rectangle(width: w, height: h):
            return w * h
            
        case let .triangle(width: w, height: h):
            return (w * h) / 2.0
            
        }
    }
    
    func perimeter() -> Double {
        switch self {
        case .point:
            return 0
            
        case let .square(side: side):
            return 4 * side
            
        case let .rectangle(width: w, height: h):
            return (2 * w) + (2 * h)
            
        case let .triangle(width: w, height: h):
            return (w + h + sqrt(pow(w, 2) + pow(h, 2)))
            
        }
    }
}

let squareShape = ShapeDimensions.square(side: 10.0)
let rectangleShape = ShapeDimensions.rectangle(width: 5.0, height: 10.0)
let pointShape = ShapeDimensions.point
let triangleShape = ShapeDimensions.triangle(width: 5.0, height: 10.0)

print("square's area = \(squareShape.area())")
print("rectangle's area = \(rectangleShape.area())")
print("point's area = \(pointShape.area())")
print("triangle's area = \(triangleShape.area())")

print("square's perimeter = \(squareShape.perimeter())")
print("rectangle's perimeter = \(rectangleShape.perimeter())")
print("point's perimeter = \(pointShape.perimeter())")
print("triangle's perimeter = \(triangleShape.perimeter())")

Edit: Third time is the charm?