<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/Layout/makeCache(subviews:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI6LayoutP9makeCache8subviews0E0QzAA0C8SubviewsV_tF"
  },
  "title" : "makeCache(subviews:)"
}
-->

# makeCache(subviews:)

Creates and initializes a cache for a layout instance.

```
func makeCache(subviews: Self.Subviews) -> Self.Cache
```

## Parameters

`subviews`

A collection of proxy instances that represent the
views that the container arranges. You can use the proxies in the
collection to get information about the subviews as you
calculate values to store in the cache.

## Return Value

Storage for calculated data that you share among
the methods of your custom layout container.

## Discussion

You can optionally use a cache to preserve calculated values across
calls to a layout container’s methods. Many layout types don’t need
a cache, because SwiftUI automatically reuses both the results of
calls into the layout and the values that the layout reads from its
subviews. Rely on the protocol’s default implementation of this method
if you don’t need a cache.

However you might find a cache useful when:

- The layout container repeats complex, intermediate calculations
  across calls like [`sizeThatFits(proposal:subviews:cache:)`](/documentation/SwiftUI/Layout/sizeThatFits(proposal:subviews:cache:)),
  [`placeSubviews(in:proposal:subviews:cache:)`](/documentation/SwiftUI/Layout/placeSubviews(in:proposal:subviews:cache:)), and
  [`explicitAlignment(of:in:proposal:subviews:cache:)`](/documentation/SwiftUI/Layout/explicitAlignment(of:in:proposal:subviews:cache:)).
  You might be able to improve performance by calculating values
  once and storing them in a cache.
- The layout container reads many [`LayoutValueKey`](/documentation/SwiftUI/LayoutValueKey) values from
  subviews. It might be more efficient to do that once and store the
  results in the cache, rather than rereading the subviews’ values before
  each layout call.
- You want to maintain working storage, like temporary Swift arrays,
  across calls into the layout, to minimize the number of allocation
  events.

Only implement a cache if profiling shows that it improves performance.

### Initialize a cache

Implement the `makeCache(subviews:)` method to create a cache.
You can add computed values to the cache right away, using information
from the `subviews` input parameter, or you can do that later. The
methods of the [`Layout`](/documentation/SwiftUI/Layout) protocol that can access the cache
take the cache as an in-out parameter, which enables you to modify
the cache anywhere that you can read it.

You can use any storage type that makes sense for your layout
algorithm, but be sure that you only store data that you derive
from the layout and its subviews (lazily, if possible). For this to
work correctly, SwiftUI needs to be able to call this method to
recreate the cache without changing the layout result.

When you return a cache from this method, you implicitly define a type
for your cache. Be sure to either make the type of the `cache`
parameters on your other [`Layout`](/documentation/SwiftUI/Layout) protocol methods match, or use
a type alias to define the [`Cache`](/documentation/SwiftUI/Layout/Cache) associated type.

### Update the cache

If the layout container or any of its subviews change, SwiftUI
calls the [`updateCache(_:subviews:)`](/documentation/SwiftUI/Layout/updateCache(_:subviews:)) method so you can
modify or invalidate the contents of the
cache. The default implementation of that method calls the
`makeCache(subviews:)` method to recreate the cache, but you can
provide your own implementation of the update method to take an
incremental approach, if appropriate.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)