some keyword in SwiftUI declares an opaque return type, meaning the function returns one specific concrete type that conforms to the protocol (like View), rather than any type that conforms. This enables protocol-associated-type usage and preserves type identity for operations like Equatable comparisons.Why this happens
SwiftUI’s View protocol has an associated type Body, which makes it a protocol with associated types (PAT). Before Swift 5.1, you couldn’t use PATs as return types directly. The some keyword—introduced in SE-0244 and shipped with Swift 5.1/Xcode 11—solves this by letting a function promise it returns one specific concrete type that conforms to the protocol, without exposing that concrete type to callers.
When you write var body: some View, you’re telling the compiler: “This property returns a single, fixed view type (like Text, VStack, or a custom view) that conforms to View.” The concrete type is inferred from the implementation, and the caller only knows it conforms to View.
This differs from returning a protocol type (-> View), which would allow returning different conforming types on different code paths—but that’s forbidden for PATs and breaks type identity needed for Equatable and similar protocols.
How to fix it
1. Declare opaque return types with some Protocol
Use some before the protocol name in return position. The implementation must return one concrete type everywhere.
struct ContentView: View {
var body: some View {
Text("Hello World")
}
}
The compiler infers the concrete type (Text here). All return paths must yield the same type:
// ✅ Valid – single concrete type
func makeView() -> some View {
Text("Hello")
}
// ❌ Error – different concrete types
func makeView(_ flag: Bool) -> some View {
if flag {
return Text("A")
} else {
return Image(systemName: "star")
}
}
2. Return protocols with associated types (PATs)
Opaque types let you return PATs like Collection or View directly—something plain protocol returns can’t do.
func giveMeACollection() -> some Collection {
[1, 2, 3] // Concrete type: Array<Int>
}
let c = giveMeACollection()
print(c.count) // 3 – Collection requirements available
3. Preserve type identity for Equatable and Comparable
Because the concrete type is fixed, two calls return values of the same type, enabling Self-requiring protocols.
func makeNumber() -> some Equatable {
42 // Inferred as Int
}
let a = makeNumber()
let b = makeNumber()
print(a == b) // ✅ Legal – both are Int
With a plain protocol return (-> Equatable), the compiler couldn’t prove a and b share the same concrete type, so == would be illegal.
4. Combine some with where clauses (Swift 5.7+)
Constrain the opaque result further without exposing the concrete type.
func integerCollection() -> some Collection where Element == Int {
[10, 20, 30]
}
How to prevent it
- Don’t mix concrete types in a single
somereturn—use@ViewBuilderor type-erasing wrappers (AnyView) if you truly need heterogeneous branches. - Avoid
AnyViewunless necessary; it erases type identity and hurts performance. - Prefer
someover protocol returns when the caller doesn’t need to know the concrete type but you need PAT support orEquatableconformance. - Remember:
somefixes the concrete type per declaration, not per call site. Two different functions returningsome Viewcan have different concrete types.
FAQ
### Can I use some in parameter position?
No. some is only allowed in return position (functions, computed properties, subscripts). For generic parameters, use the explicit <T: Protocol> syntax or the any keyword (Swift 5.6+) for existential types.
### What’s the difference between some View and any View?
some View (opaque) hides a specific concrete type chosen by the implementation. any View (existential) boxes any View-conforming value, allowing heterogeneous values but losing type identity and requiring heap allocation. Use some for performance and PAT support; use any when you genuinely need to store mixed conforming types.
### Why does SwiftUI require some View for body?
View has an associated type Body. Before opaque types, you couldn’t write var body: View because PATs couldn’t be used as types. some View satisfies the protocol requirement while keeping the concrete view type private and stable.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
