Why aren't NSFilePromiseProviderDelegate methods being called?
Why aren't NSFilePromiseProviderDelegate methods being called?
- Subject: Why aren't NSFilePromiseProviderDelegate methods being called?
- From: Charles Srstka <email@hidden>
- Date: Mon, 05 Dec 2016 15:34:22 -0600
Okay, so I decided to try to experiment with NSFilePromiseProvider, in order to replace some legacy code using old-fashioned drag-and-drop methods. However, for some reason, the drag-and-drop system seems to completely ignore my NSFilePromiseProviderDelegate methods, and calls the old-fashioned ones instead. If they’re not there, it throws an exception. What the heck?
Here’s the code:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var outlineView: NSOutlineView?
class Item: NSObject {
@objc let name: String
@objc let children: [Item]
init(name: String, children: [Item] = []) {
self.name = name
self.children = children
super.init()
}
}
@IBOutlet weak var window: NSWindow!
dynamic var items = [Item(name: "Foo"), Item(name: "Bar"), Item(name: "Baz", children: [Item(name: "Qux")])]
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.outlineView?.setDraggingSourceOperationMask(.copy, forLocal: false)
}
}
extension AppDelegate: NSOutlineViewDataSource {
func outlineView(_ outlineView: NSOutlineView, pasteboardWriterForItem item: Any) -> NSPasteboardWriting? {
return NSFilePromiseProvider(fileType: kUTTypePlainText as String, delegate: self)
}
// If this method doesn't exist, an exception is thrown on drag
func outlineView(_ outlineView: NSOutlineView, namesOfPromisedFilesDroppedAtDestination dropDestination: URL, forDraggedItems items: [Any]) -> [String] {
print("Why does this get called instead of my NSFilePromiseProviderDelegate methods?!")
try! "foo".write(to: dropDestination.appendingPathComponent("foo.txt"), atomically: true, encoding: .utf8)
return ["foo.txt"]
}
}
extension AppDelegate: NSFilePromiseProviderDelegate {
func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, fileNameForType fileType: String) -> String {
print("fileNameForType: got called with type \(fileType)")
return "foo.txt"
}
func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, writePromiseTo url: URL, completionHandler: @escaping (Error?) -> Void) {
print("writePromiseTo: got called with URL \(url)")
do {
try "foo".write(to: url, atomically: true, encoding: .utf8)
completionHandler(nil)
} catch {
completionHandler(error)
}
}
}
Output is:
Why does this get called instead of my NSFilePromiseProviderDelegate methods?!
I mean, it works, but it works using the older system. I want it to use the modern system. Why won’t it?
Charles
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden