简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM