Issue with SwiftUI onDrop: Cannot convert value of type ‘[YourDataType]’ to expected argument type ‘Binding’ [closed]

I’m trying to implement drag-and-drop functionality using SwiftUI’s onDrop modifier. However, I’m encountering a type mismatch issue when using the onDrop modifier. The error suggests that I’m passing an array [YourDataType] instead of the expected Binding.

The error message is: “Cannot convert value of type ‘[YourDataType]’ to expected argument type ‘Binding'”. Below is the relevant code snippet:

ForEach(inputList, id: \.displayName) { v in
    HStack {
        // ... other HStack content ...

        Button(action: {
            inputSchemaItemId = v.schemaItemId
            save()
        }) {
            Image(systemName: inputImageSystemName(v: v)).frame(width: 8, height: 8).foregroundColor(iconColor)
        }.onDrop(of: [UTType.text], isTargeted: nil) { providers, location in
            // Error occurs here
            inputSchemaItemId = v.schemaItemId
            save()
            return true
        } { providers, location in
            // Handle the dropped data
        }
    }
}

I’ve attempted to directly use inputList in the onDrop closure, but it seems that I need to provide a binding instead.

How can I properly use a binding in the onDrop closure with my current code structure?
Are there any modifications needed in the ForEach loop or the onDrop closure to resolve this type mismatch?

  • 1

    You have two closures for onDrop

    – 

Leave a Comment