How it works
AnyCompositorContent is a type-erasing wrapper that hides the concrete type of a piece of compositor content behind a single, uniform type. Compositor content describes the layer-renderer configuration that drives a fully immersive Metal scene, and because its types compose through generics, returning or storing different content from one place can become impossible to spell out. Wrap that content in AnyCompositorContent when you need a stable, homogeneous type — for example to return varying content from a single property, hold it in a collection, or pass it across an API boundary that can't name the underlying generic type.
Wrap any content with the AnyCompositorContent initializer
The initializer takes a value conforming to CompositorContent and erases its static type, yielding a single AnyCompositorContent value regardless of what went in. In the example the wrapped content is the layer renderer shown as
AnyCompositorContent { layerRenderer }— whatever its concrete type, the result is uniformly typed.Conform to CompositorContent
AnyCompositorContent itself conforms to CompositorContent, so an erased value is still legal compositor content and can flow wherever the protocol is expected. This is what lets you substitute the wrapper for the original without breaking the content pipeline that the renderer consumes.
Unify branches under one return type
Because every wrapped value shares the type AnyCompositorContent, you can choose between alternative content at runtime and still satisfy a single declared type. Use it as the return type of a property or function that must produce different content along different paths, rather than fixing one generic shape.
Hand the erased content to the layer renderer
Once erased, the value plugs into the same place the original content would — the configuration that feeds an immersive LayerRenderer. The
layerRenderernamed in the example caption stands in for that consumer; AnyCompositorContent is simply the type it accepts when the source type must stay hidden.
AnyCompositorContent { layerRenderer } — the call site keeps compiling because both branches share the one erased type.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct AnyCompositorContentDemo: View {
var body: some View {
VStack(spacing: 12) {
Image(systemName: "visionpro")
.font(.system(size: 44))
.foregroundStyle(.tint)
Text("AnyCompositorContent")
.font(.headline)
Text("Type-erases compositor content for an immersive layer renderer.")
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
Text("AnyCompositorContent { layerRenderer }")
.font(.system(.footnote, design: .monospaced))
.padding(8)
.background(.quaternary, in: RoundedRectangle(cornerRadius: 6))
}
.padding()
}
}