Macos 拖放到NSArrayController

Macos 拖放到NSArrayController,macos,swift,Macos,Swift,我正在努力将从拖放(finder到NSView的文件)接收的变量传递到AppDelegate中的NSArrayController NSView接收拖放操作: class MyView:NSView, NSDraggingDestination { override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) } let fileTypes = ["jpg", "jpeg",

我正在努力将从拖放(finder到NSView的文件)接收的变量传递到AppDelegate中的NSArrayController

NSView接收拖放操作:

class MyView:NSView, NSDraggingDestination {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
    }

    let fileTypes = ["jpg", "jpeg", "bmp", "tif", "TIF"]
    var fileTypeIsOk = false
    var droppedFilePath = ""


    required init?(coder: NSCoder) {
        let types = [NSFilenamesPboardType, NSURLPboardType, NSPasteboardTypeTIFF]
        super.init(coder: coder)
        registerForDraggedTypes(types)
    }
    override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation {

        if checkExtension(sender) == true {
            fileTypeIsOk = true
            return .Every
        } else {
            fileTypeIsOk = false
            println("Dropped images rejected.")
            return .None
        }
    }
    override func performDragOperation(sender: NSDraggingInfo) -> Bool {

        var go = false
        if let board =  sender.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray {            
            for indexFile in 0...board.count-1 {
                println("Files dropped \(board[indexFile])")
                //-> pass the variable board[indexFile] or board[] to NSArrayController

                go = true
            }
        }
        return go
    }

    func checkExtension(drag: NSDraggingInfo) -> Bool {
        var go = false
        var foundExtension = 0
        var numberOfFiles = 0

        if let board = drag.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray {
            numberOfFiles = board.count
            for indexFile in 0...numberOfFiles-1 {
                if let url = NSURL(fileURLWithPath: (board[indexFile] as! String)) {
                    let suffix = url.pathExtension!
                    for ext in fileTypes {
                        if ext.lowercaseString == suffix {
                            ++foundExtension
                            break
                        }
                    }
                }
            }
        }
        if foundExtension == numberOfFiles {
            println("Dropped files have valid extension.")
            go = true
        } else {
            println("At least one file dropped has an invalid extension.")
        }
        return go
    }
}
在AppDelegate中,我设置了一个NSArrayController(目前指向数据[],但我不知道如何从board[]填充数据[])

感谢您的帮助

在您的NSArrayController上调用“-(void)addObjects:(NSArray*)objects;”方法并将其传递到板上:

[self.myArrayController addObjects:board];

它通过添加NSView:performDragOperation:

让appDelegate=NSApplication.sharedApplication()。委托为!AppDelegate


然后我可以使用appDelegate.data访问'data'变量。

谢谢,问题是我在写入:self.droppedFilesControler.addObject(board)appDelegate时出错。appDelegate无法识别'board'Board'是在类MyView中创建的。您需要将阵列控制器放入NSView或从AppDelegate中公开它(通过在AppDelegate的头文件中将其声明为@属性)。在swift中没有.h文件。我想从AppDelegate选项公开ArrayController,但不确定如何在swift中编写它。再次感谢你的帮助。
[self.myArrayController addObjects:board];