0Pricing
Swift Academy · Lesson

Layout Cache and Performance

Use the layout cache for efficiency.

Why a Layout Cache?

SwiftUI may call sizeThatFits and placeSubviews many times during a single layout pass.

If your calculations are expensive, a cache lets you compute once and reuse, avoiding repeated work.

The Cache Associated Type

The Layout protocol has an associated Cache type, defaulting to Void. Define your own struct to store precomputed results.

struct FlowLayout: Layout {
    struct CacheData {
        var sizes: [CGSize] = []
    }
    typealias Cache = CacheData
}

All lessons in this course

  1. The Layout Protocol Basics
  2. Measuring Subviews
  3. Building a Flow Layout
  4. Layout Cache and Performance
← Back to Swift Academy