Swift says 'any View' cannot conform to 'View'
Type 'any View' cannot conform to 'View'Short answer
View has an associated type, so a boxed any View value has no single concrete body type and cannot stand in for a generic parameter. Swift only opens the box in simple calls. Use some View or a generic parameter instead of any View, and when a container hides the value, move the work into a generic function.
Fix it fast
1Find out which cause you have
Find the line and note whether any View is passed on its own, or wrapped inside an array, an optional or a generic type.
xcodebuild -scheme MyApp build 2>&1 | grep -B2 -A6 "any View"2Match what you see, then jump to its fix
What does the error look like?
Type 'any View' cannot conform to 'View'
Cause 1 of 2
A protocol with an associated type is used as a plain value type
1Check
Per the Swift proposal that introduced any, an existential of a protocol with an associated type cannot conform to the protocol itself, because no concrete associated type fits every value.
grep -rn "any View" Sources/2Fix
Use a generic parameter or an opaque type, which keep the concrete type known at compile time.
// before
func card(_ content: any View) -> some View { Card(content: content) }
// after
func card<Content: View>(_ content: Content) -> some View { Card(content: content) }3Verify
The file compiles and the preview renders.
xcodebuild -scheme MyApp buildConfirmed by raw.githubusercontent.com
Cause 2 of 2
The box cannot be opened because it sits inside another type
1Check
Since SE-0352, Swift opens an any value when it is passed directly to a T parameter. It does not open it when T appears in an array, an optional or another generic type, or in a position that returns a type depending on T.
grep -rn "\[any View\]\|any View?" Sources/2Fix
Move the code that needs the concrete type into a generic function and call it with the element on its own.
func render<V: View>(_ view: V) -> some View {
view.padding()
}
// call per element, not on the array
render(item)3Verify
The call compiles because the argument is a single any View value passed straight to a generic parameter.
xcodebuild -scheme MyApp buildConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Prefer some View and generic parameters for SwiftUI building blocks.
- Use any View only where you truly need to store different view types together.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
UserDefaults logs 'Failed to read values in CFPrefsPlistSource' for an app group
Settings saved by the app are not visible to its extension, widget or sibling app that share the group
This log comes from the preferences system when UserDefaults is opened for an app group's suite.
3 causes, each with a fixMobile apps
iOS app crashes: Google Maps SDK must be initialized via provideAPIKey
Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
The Maps SDK throws this when a Maps object is created before GMSServices.provideAPIKey has run.
3 causes, each with a fixMobile apps
UIKit app crashes because it cannot find the storyboard
Could not find a storyboard named 'Main' in bundle NSBundle
UIStoryboard throws this exception when no storyboard file with that name exists in the bundle it searches.
3 causes, each with a fixMobile apps
Firebase on iOS asks for the FCM token before APNs has given one
Be sure to re-retrieve the FCM token once the APNS device token is set.
The Firebase Messaging SDK needs the APNs device token from iOS before it hands out an FCM token.
3 causes, each with a fixMobile apps
A test push from the Firebase console never shows up on the iPhone
didReceiveRegistrationToken prints a token, yet no notification arrives
When a console message is accepted but the iPhone stays silent, the break is between Firebase and APNs or inside the app.
4 causes, each with a fixMobile apps
InstanceID token() no longer works in a Swift Firebase app
Instance ID calls fail to compile after upgrading Firebase to 8.0.0 or later
Firebase removed the Instance ID dependency from Messaging in 8.0.0, so InstanceID.instanceID() calls no longer belong in a current app.
3 causes, each with a fix