2

我创建了一个大纲视图表,它显示了我的磁盘(文件系统)层次结构,类似于 Apple 的这个示例:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/OutlineView/Articles/UsingOutlineDataSource.html#//apple_ref/doc/uid/20000725-142693

现在我希望能够将文件/文件夹从我的大纲视图拖到其他接受 drop 的应用程序,如 Finder、Xcode、iTunes 等。这些是我为 NSou 的数据源实现的功能

let pb: NSPasteboard?
func outlineView(outlineView: NSOutlineView, writeItems items: [AnyObject], toPasteboard pasteboard: NSPasteboard) -> Bool {
    var array = [NSURL]()
    self.pb?.declareTypes([NSFilesPromisePboardType], owner: self)
    if let fileItem = items[0] as? FileSystemItem {
        let fileURL = NSURL(fileURLWithPath: fileItem.getFullPath()!)
        array.append(fileURL)
        self.pb?.addTypes([fileURL.pathExtension!], owner: nil)
        self.pb?.writeObjects(array)
        return true
    }else {
        return false
    }
}
func outlineView(outlineView: NSOutlineView, namesOfPromisedFilesDroppedAtDestination dropDestination: NSURL, forDraggedItems items: [AnyObject]) -> [String] {
    var names = [String]()
    if let fileItem = items[0] as? FileSystemItem {
        print(fileItem.getRelativePath())
        names.append(fileItem.getRelativePath()!)
        return names
    }else {
        return names
    }
}

我目前在控制台中收到此错误

Looked for HFSPromises on the pasteboard, but found none.

最后这是我所拥有的截图 - NSOutlineView 显示文件系统

4

1 回答 1

1

除非文件不存在并且您将在收件人指定的位置创建它,否则不要使用承诺。如果是单个文件,使用 NSURLPboardType。对于多个文件,使用 NSFilenamesPboardType。

于 2016-08-03T16:59:07.817 回答