Runtime crash in Swift 2 file drag-and-drop code

Since converting a project from Swift 1.2 to Swift 2, I'm getting a crash (EXC_BAD_ACCESS) when dropping files into a drop zone. Here's the code:


override func performDragOperation(sender: NSDraggingInfo) -> Bool
{
     let pboard: NSPasteboard = sender.draggingPasteboard()
     if let urls = pboard.readObjectsForClasses([NSURL.self], options:nil) {
          let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
          appDelegate.performActionForDropZone(self.identifier!, withDroppedItems: urls as! [NSURL])
     }
     return true
}

It's crashing in pboard.readObjectsForClasses() when I drop 1 or more files (and/or folders) into the drop zone, which triggers this function. This was running fine in Swift 1.2 (I'd get an array of URLs sent to the app delegate), and I'm at a loss to understand why it's crashing now, other than it might be a Swift 2 runtime problem. Any thoughts or suggestions would be greatly appreciated.

Answered by OOPer in 30108022

It seems this issue is fixed in Xcode 7 beta 4. Try it with your project.

Did you ever get this resolved? I am having a similar issue with the general pasteboard with readObjectsForClasses.

I too am seeing this issue. I had to make a change in what I implemented.


Instead of using pboard.readObjectsForClasses() in my performDragOperation(:), I did something like the following as a workaround:


override func performDragOperation(sender: NSDraggingInfo) -> Bool {

let pboard : NSPasteboard = sender.draggingPasteboard()

for pboardItem in pboard.pasteboardItems! {

let temp = pboardItem.stringForType("public.utf8-plain-text")!

Swift.print("TheDropString: \(temp)")

}

return true

}


I would rather use pboard.readObjectsForClasses(). But for now I'll use the above to finish out my prototyping.


If someone does find an answer regarding readObjectsForClasses(), please share.

Can't this be a simple workaround?

        let pboard = sender.draggingPasteboard()
        let classes: NSArray = [NSURL.self]
        if let urls = pboard.readObjectsForClasses(classes as! [AnyClass], options:nil) {
            //...
        }

It seems current Swift 2 runtime has a bug around bridging native [AnyClass] array to Objective-C NSArray.

Better send Bug Report as soon as possible.

Accepted Answer

It seems this issue is fixed in Xcode 7 beta 4. Try it with your project.

Thanks - my code now works in beta 4!

Runtime crash in Swift 2 file drag-and-drop code
 
 
Q