Swift 4: NSFilenamesPboardType is not available. What to use for registerForDraggedTypes?

After switching to Swift4, the following code causes a compilation error:

public final class MediaItemView: NSView { public override init(frame frameRect: NSRect) { super.init(frame: frameRect) // error: 'NSFilenamesPboardType' is unavailable in Swift: // use 'NSPasteboard.writeObjects(_:)' with file URLs let draggedTypes: [NSPasteboard.PasteboardType] = [NSFilenamesPboardType] registerForDraggedTypes(draggedTypes) } } 

What is the replacement for NSFilenamesPboardType in Swift4? How to register drag type file name (in my case mp3, wav, aiff, ... files) in Swift4?

Thanks!

+13
cocoa swift drag-and-drop swift4 macos
source share
6 answers

I decided backward compatibility with this extension:

 extension NSPasteboard.PasteboardType { static let backwardsCompatibleFileURL: NSPasteboard.PasteboardType = { if #available(OSX 10.13, *) { return NSPasteboard.PasteboardType.fileURL } else { return NSPasteboard.PasteboardType(kUTTypeFileURL as String) } } () } 

That means you can use NSPasteboard.PasteboardType.backwardsCompatibleFileURL

+16
source share

i use this as a solution

  //Temp solution for this let NSFilenamesPboardTypeTemp = NSPasteboard.PasteboardType("NSFilenamesPboardType") self.zipView.registerForDraggedTypes([NSFilenamesPboardTypeTemp]) 

this seems to be a bug from the apple, they marked api as working only in 10.13.

I am running an error for apple :)

+10
source share

I like the creative workarounds presented here for the deprecated variable NSFilenamesPboardType . After exploring this question, you can use the readObjects(forClasses:options:) way to move forward with an equivalent non-wood approach. It will also be safer if WRT can work on future macOS. It will be implemented, as in the following example, tested with Swift 4.1, based on registering NSView in the storyboard.

 override func awakeFromNib() { registerForDraggedTypes([.fileURL]) } override func draggingEnded(_ sender: NSDraggingInfo) { sender .draggingPasteboard() .readObjects(forClasses: [NSURL.self], options: nil)? .forEach { // Do something with the file paths. if let url = $0 as? URL { print(url.path) } } } 

Since the class array parameter for readObjects is of type [AnyClass] , this is the reason for using NSURL instead of URL .

+6
source share

I also run into the same problem and my solution creates a custom NSPasteboard.PasteboardType with kUTTypeURL . I'm not sure if this is the most correct way (and I suppose not), but it works, at least for a temporary workaround.

  let draggedType = NSPasteboard.PasteboardType(kUTTypeURL as String) self.tableView?.registerForDraggedTypes([draggedType]) 

In addition, the new NSPasteboard.PasteboardType has a .fileNameType(forPathExtension: "foo") method .fileNameType(forPathExtension: "foo") . You must try. However, for some reason this does not work in my case.

+3
source share

Using a combination of Mark Bridges answer and slboat answer, I came to the following solution:

 extension NSPasteboard.PasteboardType { /// The name of a file or directory static let fileName: NSPasteboard.PasteboardType = { return NSPasteboard.PasteboardType("NSFilenamesPboardType") }() } 

This works as expected in my testing.

+1
source share

Swift4, Swift5 :

Well, partly a temporary workaround, but working on Swift4 / 5:

 var chromeType: NSPasteboard.PasteboardType { return NSPasteboard.PasteboardType.init(rawValue: "org.chromium.drag-dummy-type") } var finderNode: NSPasteboard.PasteboardType { return NSPasteboard.PasteboardType.init(rawValue: "com.apple.finder.node") } var fileURLs: NSPasteboard.PasteboardType { return NSPasteboard.PasteboardType.init(rawValue: "NSFilenamesPboardType") } var webarchive: NSPasteboard.PasteboardType { return NSPasteboard.PasteboardType.init(rawValue: "com.apple.webarchive") } 

then aggregate as

 var acceptableTypes: Set<NSPasteboard.PasteboardType> { return [.URL, .fileURL, .pdf, .png, .rtf, .rtfd, .tiff, finderNode, webarchive] } 

then use the view loaded method:

 // Intercept drags registerForDraggedTypes(Array(acceptableTypes)) 
0
source share

All Articles