Search code examples
swiftmacosdrag-and-dropnspasteboardswift4

Drag and Drop using swift 4 on mac OS


I'm using swift 4 and Xcode 9 and I have a problem with implementation of drag and drop. I have a custom 'destination' view for drops and in swift 3 i call

register(forDraggedTypes: Array(NSURLPboardType))

to accept drags that contain those types. How can I do somethings like this in swift 4? Now I have this code

registerForDraggedTypes([.pdf])

And no one NSDraggingDestination method calls when I drop a pdf in my view. (my custom view sits on top)


Solution

  • So my colleague found solution of this problem, in swift 4 you have to use kUTTypes casted as String for drag and drop, like this:

    registerForDraggedTypes([NSPasteboard.PasteboardType(rawValue: kUTTypeFileURL as String), NSPasteboard.PasteboardType(rawValue: kUTTypeItem as String)])
    

    With this code all NSDraggingDestination methods works fine, you can drop any file from finder in your view.