我从哪里获得namesOfPromisedFilesDroppedAtDestination

时间:2016-07-23 15:03:27

标签: swift macos cocoa drag-and-drop

我正在尝试从Safari拖动NSPromise图像获取NsUrl / NSData。

用于拖放的Apple文档似乎很不清楚。 它说我应该假设有一个名为Droplocation的属性,但没有说: - 这是什么 - 它来自哪里 - 以及如何获得它。

NSView没有drop属性 - 所以在我们被要求假设它存在之前知道我们从哪里得到它会很有用。

任何人都可以帮助我理解我可以从NSView获取下面代码段中的dropLocation吗?

链接到文档: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DragandDrop/Tasks/DraggingFiles.html

NSURL *dropLocation; // Assume this exists

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];

    if ( [[pboard types] containsObject:NSFilesPromisePboardType] ) {
        NSArray *filenames = [sender
                namesOfPromisedFilesDroppedAtDestination:dropLocation];
        // Perform operation using the files’ names, but without the
        // files actually existing yet
    }
    return YES;
}

获取上述代码的工作版本的任何帮助?

1 个答案:

答案 0 :(得分:0)

感谢评论的帮助,我能够让它工作。

以下代码对我有用:

if let pastetype = pasteboard.types {

            let tempDir = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)

            if pastetype.contains("NSPromiseContentsPboardType") {

                //NSPromise
            let destination = sender.namesOfPromisedFilesDroppedAtDestination(tempDir)

                let tempdestination = tempDir.URLByAppendingPathComponent(destination![0])
                let userInfo:[String:NSURL] = ["fileUrl":tempdestination]

                //I send the resulting file to my viewcontroller using notification. 
            NSNotificationCenter.defaultCenter().postNotificationName(imageDroppedNotification, object: self, userInfo: userInfo)

            }
        }