我已经为 Mail 工作了一段时间的拖放工作。直到我升级到 OSX 10.13。
这是我的代码:
class DropView: NSView
{
var filePath: String?
required init?(coder: NSCoder) {
super.init(coder: coder)
self.wantsLayer = true
self.layer?.backgroundColor = NSColor.red.cgColor
registerForDraggedTypes([NSPasteboard.PasteboardType
.fileNameType(forPathExtension: ".eml"), NSPasteboard.PasteboardType.filePromise])
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
if true
{
self.layer?.backgroundColor = NSColor.blue.cgColor
return .copy
}
}
override func draggingExited(_ sender: NSDraggingInfo?)
{
self.layer?.backgroundColor = NSColor.red.cgColor
}
override func draggingEnded(_ sender: NSDraggingInfo)
{
self.layer?.backgroundColor = NSColor.gray.cgColor
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool
{
let pasteboard: NSPasteboard = sender.draggingPasteboard()
let filePromises = pasteboard.readObjects(forClasses: [NSFilePromiseReceiver.self], options: nil) as? [NSFilePromiseReceiver]
let folderPath = NSHomeDirectory()+"/Drop Stuff/"
if (!FileManager.default.fileExists(atPath: folderPath))
{
do
{
try FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil)
}
catch
{
print ("error")
}
}
let folderURL = NSURL(fileURLWithPath: folderPath)
let f = sender.namesOfPromisedFilesDropped(atDestination: folderURL as URL)
print (f!)
print ("Copied to \(folderPath)")
return true
}
}
问题是 namesOfPromisedFilesDropped 返回父文件夹的名称,而不是文件的名称,就像它在以前版本的操作系统上所做的那样。
编译器警告 namesOfPromisedFilesDropped 已弃用。Apple 做得很好,因为它没有提供任何关于新东西的文档。多亏了 StackOverflow,我设法将它拼凑在一起,它可以使用新的 API,但仍然表现出与上述相同的问题。
class DropView2: NSView
{
var filePath: String?
required init?(coder: NSCoder) {
super.init(coder: coder)
self.wantsLayer = true
self.layer?.backgroundColor = NSColor.red.cgColor
registerForDraggedTypes([NSPasteboard.PasteboardType
.fileNameType(forPathExtension: ".eml"), NSPasteboard.PasteboardType.filePromise])
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
if true
{
self.layer?.backgroundColor = NSColor.blue.cgColor
return .copy
}
}
override func draggingExited(_ sender: NSDraggingInfo?)
{
self.layer?.backgroundColor = NSColor.red.cgColor
}
override func draggingEnded(_ sender: NSDraggingInfo)
{
self.layer?.backgroundColor = NSColor.gray.cgColor
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool
{
let pasteboard: NSPasteboard = sender.draggingPasteboard()
guard let filePromises = pasteboard.readObjects(forClasses: [NSFilePromiseReceiver.self], options: nil) as? [NSFilePromiseReceiver] else {
return false
}
print ("Files dropped")
var files = [URL]()
let filePromiseGroup = DispatchGroup()
let operationQueue = OperationQueue()
let destURL = URL(fileURLWithPath: "/Users/andrew/Temporary", isDirectory: true)
print ("Destination URL: \(destURL)")
filePromises.forEach ({ filePromiseReceiver in
print (filePromiseReceiver)
filePromiseGroup.enter()
filePromiseReceiver.receivePromisedFiles(atDestination: destURL,
options: [:],
operationQueue: operationQueue,
reader:
{ (url, error) in
print ("Received URL: \(url)")
if let error = error
{
print ("Error: \(error)")
}
else
{
files.append(url)
}
print (filePromiseReceiver.fileNames, filePromiseReceiver.fileTypes)
filePromiseGroup.leave()
})
})
filePromiseGroup.notify(queue: DispatchQueue.main,
execute:
{
print ("Files: \(files)")
print ("Done")
})
return true
}
}
我正在使用 10.13.2。我做错了什么还是这是一个错误?
它快把我逼疯了。