Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type 'any View' cannot conform to 'View' when using Gesture via function

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
            }
        })
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is a lot of difference between the keywords some & any. some is returning any type of Gesture that doesn’t change, like if it is a DragGesture, it should always be that. Whereas any returns a type of Gesture that is not set to always be DragGesture gesture for example.

In your case, replacing any with some 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading