I’m trying to extract my gesture out to a function for use within one of my Swift Packages. The issue I’m having is that when I attempt to use it on one of my views, it doesn’t conform to View anymore.
The following code produces this error: Type 'any View' cannot conform to 'View'
struct ContentView: View {
var body: some View {
VStack {
Text("Placeholder")
}
.gesture(swipeDownGesture())
}
func swipeDownGesture() -> any Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .local).onEnded({ gesture in
if gesture.translation.height > 0 {
// Run some code
}
})
}
}
2
Answers
use
some
instead,some
indicates for compiler to infer concrete type from whatever is generated and returned inside:There is a lot of difference between the keywords
some
&any
.some
is returning any type ofGesture
that doesn’t change, like if it is aDragGesture
, it should always be that. Whereas any returns a type ofGesture
that is not set to always beDragGesture
gesture for example.In your case, replacing
any
withsome
does the trick.Edit:
any
is more like a cast working as a Type eraser.some
is for associated type, acting more like generics. For more info, check this out.