Ian Warburton
Ian Warburton

Reputation: 15676

How to use .onDrag without implementing NSItemProviderWriting

I want to call .onDrag and pass it a custom object and I don't want to have to implement NSItemProviderWriting because it's complicated.

UIKit has a class called UIDragItem that has a property localObject. Is it possible to use this or an equivalent with SwiftUI?

Upvotes: 1

Views: 350

Answers (1)

Ian Warburton
Ian Warburton

Reputation: 15676

I added a UUID id to my object and created an NSItemProvider from that.

.onDrag {

    NSItemProvider(object: NSString(string: self.clip.id.uuidString))
}

Then I can locate the entity using this Id on a drop.

Even easier is to ignore the NSItemProvider stuff and pass a closure to a drop delegate...

.onDrag(of: [], delegate: ClipDropDelegate(f: { self.viewModel.changePosition(clip: self.clip) }))

Upvotes: 3

Related Questions