首页 文章

如何将文件夹/目录项从NSOutlineView拖动到像Finder或Xcode这样的应用程序

提问于
浏览
2

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

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

现在我希望能够将文件/文件夹从我的大纲视图拖到其他接受像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 showing file system

1 回答

  • 1

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

相关问题