Chapter 3. Protocols 의 후속 공부

<aside> 📌 목차

1. Allocation

= 연산자에서의 차이

→ heap allocation 은 비싼 동작이다.

아래 코드를 보자

enum Color {
  case blue, green, gray
}

enum Orientation {
  case left, right
}

enum Tail {
  case none, tail, bubble
}

// Never construct something more than once; if you need it again, it’s already here.
var cache = [String: UIImage]()

func makeBalloon(_ color: Color, orientation: Orientation, tail: Tail) -> UIImage {
  // Add
  let key = “\\(color):\\(orientation):\\(tail)”
  if let image = cache[key] {
    return image
  }
  // If not cached, make a new balloon...
}

what’s bad?

How can you make this better?