Swift 4粘贴板到查找器

Swift 4粘贴板到查找器,swift,swift4,Swift,Swift4,MacOS 10.12上的Finder接受哪些粘贴板类型? 下面的代码让我们拖动到其他应用程序(如Terminal和Sublime),但Finder不接受它。这是粘贴板类型的问题,还是我遗漏了什么 override func viewDidLoad() { super.viewDidLoad() mediaInUseTableView.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: false) }

MacOS 10.12上的Finder接受哪些粘贴板类型? 下面的代码让我们拖动到其他应用程序(如Terminal和Sublime),但Finder不接受它。这是粘贴板类型的问题,还是我遗漏了什么

override func viewDidLoad() {
    super.viewDidLoad()
    mediaInUseTableView.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: false)
}
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
    var urls = [NSURL]()
    var types = [NSPasteboard.PasteboardType]()
//        types.append(NSPasteboard.PasteboardType(kUTTypeURL as String))
//        types.append(NSPasteboard.PasteboardType("NSURLPboardType"))
    types.append(NSPasteboard.PasteboardType("NSFilenamesPboardType"))
//        types.append(NSPasteboard.PasteboardType.string)
    for row in rowIndexes{
        urls.append(self.mediaInUses[row].url.absoluteURL as NSURL)
//            types.append(NSPasteboard.PasteboardType.fileNameType(forPathExtension: self.mediaInUses[row].url.pathExtension))
    }
    pboard.declareTypes(types, owner: nil)
    pboard.writeObjects(urls)
    return true
}
在我的环境中,有两种更具启发性的类型似乎不可用:

NSFilenamesPboardType
'NSFilenamesPboardType' is unavailable in Swift: use 'PasteboardType.fileURL'
NSPasteboard.PasteboardType.fileURL
'fileURL' is only available on OS X 10.13 or newer

我花了数小时分析Finder自己的拖放数据,并在.declareTypes、.setPropertyList和.setData中尝试了无数不同的对象。我让它工作一次(!),然后在同一代码上再次中断。 我也在绝望中切换到Swift 3.2。今天,我意识到,.writeObjects应该根据其输入自动执行上述所有操作,并测试了一个最小的实现,该实现与其他应用程序配合良好。当这在Finder中不起作用时,我确信问题出在其他地方

假设:由于无效的尝试,Finder将我的应用程序列入黑名单

解决方案:重新启动计算机,突然Finder接受了拖动

昨天我也重新启动了几次,但当时我的实现可能很糟糕

最低限度的实施(Swift 3/4)

// Enable drag to other applications:
tableView.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: false)
// Serve data for dragged table rows:
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
    // Prepeare data:
    var arrayOfNSURLs = [NSURL]()
    for rowIndex in rowIndexes{
        arrayOfNSURLs.append(self.mediaFiles[rowIndex].url.absoluteURL as NSURL)
    }
    // Let API write objects automatically:
    pboard.writeObjects(arrayOfNSURLs)
    return true
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Enable global drag (to other applications)
    mediaFilesTableView.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: false)
    sourceClipsTableView.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: false)
}
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
    if tableView == self.mediaFilesTableView {
        var arrayOfNSURLs = [NSURL]()
        for row in rowIndexes{
            arrayOfNSURLs.append(self.mediaFiles[row].url.absoluteURL as NSURL)
        }
        pboard.writeObjects(arrayOfNSURLs)
        return true
    }
    if tableView == self.sourceClipsTableView {
        var names = [NSString]()
        var info = ""
        for row in rowIndexes{
            info = "\(self.sourceClips[row].mediaFiles.count)"
            if info == "0"{
                info = "MISSING"
            }
            names.append(self.sourceClips[row].name.padding(toLength: 30, withPad: " ", startingAt: 0) + info as NSString)
        }
        pboard.writeObjects(names)
        return true
    }
    return false
}
以下是我的完整实施(Swift 3/4)

// Enable drag to other applications:
tableView.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: false)
// Serve data for dragged table rows:
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
    // Prepeare data:
    var arrayOfNSURLs = [NSURL]()
    for rowIndex in rowIndexes{
        arrayOfNSURLs.append(self.mediaFiles[rowIndex].url.absoluteURL as NSURL)
    }
    // Let API write objects automatically:
    pboard.writeObjects(arrayOfNSURLs)
    return true
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Enable global drag (to other applications)
    mediaFilesTableView.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: false)
    sourceClipsTableView.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: false)
}
func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
    if tableView == self.mediaFilesTableView {
        var arrayOfNSURLs = [NSURL]()
        for row in rowIndexes{
            arrayOfNSURLs.append(self.mediaFiles[row].url.absoluteURL as NSURL)
        }
        pboard.writeObjects(arrayOfNSURLs)
        return true
    }
    if tableView == self.sourceClipsTableView {
        var names = [NSString]()
        var info = ""
        for row in rowIndexes{
            info = "\(self.sourceClips[row].mediaFiles.count)"
            if info == "0"{
                info = "MISSING"
            }
            names.append(self.sourceClips[row].name.padding(toLength: 30, withPad: " ", startingAt: 0) + info as NSString)
        }
        pboard.writeObjects(names)
        return true
    }
    return false
}
编辑:切换回Swift 4,这些行似乎与Swift 3中的相同