Andy Davison
Andy Davison

Reputation: 591

SwiftUI Drag&Drop: Accessing the dragged item in validateDrop

G'day everyone,

I'm trying to interrogate the dragged object during the validate phase of drag and drop in SwiftUI, but haven't been able to make it work. Here's my validateDrop method:

func validateDrop(info: DropInfo) -> Bool {
    print("Drop Validating")
    print ("Dropping into slot where  \(rosterPlayer!.givenName!) \(rosterPlayer!.surname!) is")


    guard info.hasItemsConforming(to: [PlayerShirtUTT.uti.identifier]) else {
        return false
    }

    let itemProviders = info.itemProviders(for: [PlayerShirtUTT.uti.identifier])
        guard let itemProvider = itemProviders.first
    else {
        return false
    }
    print ("Lets check")
    
    itemProvider.loadObject(ofClass: Player.self) { player, _ in
        let player = player as? Player
        // Do the player checks here
        print ("Player check code")
    }
    return true
}

Exactly the same code in my performDrop method works fine... can anyone point me in the right direction here please?

Upvotes: 1

Views: 359

Answers (1)

Andy Davison
Andy Davison

Reputation: 591

If anyone else is looking for an answer to this, the answer is you can't.

Documentation reference:

https://developer.apple.com/documentation/uikit/drag_and_drop/making_a_view_into_a_drop_destination

The content of the dragged item is only available asynchronously after the drop is concluded (i.e. only in perdormDrop() ).

So, now I'm looking at ways to use the UTT as a distinguishing identifier for the same base data.

Upvotes: 1

Related Questions