swift - 打开文件对话框在 Swift 中崩溃

标签 swift xcode nsfilemanager nsopenpanel

我想使用 NSFilemanager 的打开文件对话框,但我的代码有时会崩溃,有时会工作,我不知道为什么。有时它 100% 工作,有时窗口是空的,有时窗口中显示对话框背后的背景。发生崩溃时,Xcode 中会显示“signal:SIGABRT”。

func openfiledlg (title: String, message: String) -> String
{
    var myFiledialog: NSOpenPanel = NSOpenPanel()

    myFiledialog.prompt = "Öffnen"
    myFiledialog.worksWhenModal = true
    myFiledialog.allowsMultipleSelection = false
    myFiledialog.canChooseDirectories = false
    myFiledialog.resolvesAliases = true
    myFiledialog.title = title
    myFiledialog.message = message
    myFiledialog.runModal()
    var chosenfile = myFiledialog.URL
    if (chosenfile != nil)
    {
        var TheFile = chosenfile.absoluteString!
        return (TheFile)
    }
    else
    {
        return ("")
    }
}

我做错了什么?为什么会崩溃?

应用程序不在主线程上运行。我总是打开一个新线程来运行我的程序。主线程仅处理来自 SpriteKit 的屏幕更新,我将其用于我的程序。

我刚刚构建了一个新的基于 Cocoa 的应用程序,并让该函数在主线程中运行并在那里运行。当我在 Cocoa-App 中启动一个线程时,它会像在 SpriteKit 环境中一样崩溃。

我需要在 Sprite-Kit 环境中启动一个新线程,因为如果我直接从 AppDelegate 启动我的主程序,更新将不会完成。主程序一直运行到整个 SpriteKit 退出,所以我没有机会在主线程中完成我的工作。

崩溃发生在 runModal() 行,然后是“NSSavePanel._spAuxiliaryStorage”:

0x7fff84dfec20:  movq   -0x10c18197(%rip), %rsi   ; "_refreshDelegateOptions"
0x7fff84dfec27:  movq   %rbx, %rdi
0x7fff84dfec2a:  callq  *%r15
0x7fff84dfec2d:  movq   -0x10c17ed4(%rip), %rsi   ; "_loadPreviousModeAndLayout"
0x7fff84dfec34:  movq   %rbx, %rdi
0x7fff84dfec37:  callq  *%r15
0x7fff84dfec3a:  movq   -0x10b57079(%rip), %r12   ; NSSavePanel._spAuxiliaryStorage <--- Thread 7: signal SIGABRT
0x7fff84dfec41:  movq   (%rbx,%r12), %rax
0x7fff84dfec45:  movq   -0x10b5716c(%rip), %rcx   ; NSSavePanelAuxiliary._clientSetADirectory
0x7fff84dfec4c:  movb   (%rax,%rcx), %al
0x7fff84dfec4f:  shrb   $0x2, %al
0x7fff84dfec52:  andb   $0x1, %al
0x7fff84dfec54:  xorb   $0x1, %al
0x7fff84dfec56:  movzbl %al, %ecx
0x7fff84dfec59:  movq   -0x10c18310(%rip), %rsi   ; "_configureForDirectory:forceDefault:"
0x7fff84dfec60:  movq   %rbx, %rdi
0x7fff84dfec63:  xorl   %edx, %edx
0x7fff84dfec65:  callq  *%r15
0x7fff84dfec68:  movq   -0x10c2d767(%rip), %rsi   ; "drain"
0x7fff84dfec6f:  movq   %r14, %rdi
0x7fff84dfec72:  callq  *%r15
0x7fff84dfec75:  movq   (%rbx,%r12), %rsi

在终端窗口中显示:

CocoaTest(54483,0x106f78000) malloc: *** error for object 0x60800017efc0: Heap corruption detected, free list canary is damaged

知道如何在不在主线程中执行的情况下解决这个问题吗?

最佳答案

当您使用以下代码时,可以解决非线程保存的用户界面 (UI) 调用的限制,该代码在主线程中异步执行命令 block :

dispatch_async(dispatch_get_main_queue())
{
    // This commands are executed ansychronously
}

所以你必须为每个内置函数编写你自己的函数,这不是像这样的线程保存(打开文件对话框的例子):

func not (b: Bool) -> Bool
{
    return (!b)
}

func suspendprocess (t: Double)
{
    var secs: Int = Int(abs(t))
    var nanosecs: Int = Int(frac(abs(t)) * 1000000000)
    var time = timespec(tv_sec: secs, tv_nsec: nanosecs)
    let result = nanosleep(&time, nil)
}

func openfiledialog (windowTitle: String, message: String, filetypelist: String) -> String
{
    var path: String = ""
    var finished: Bool = false

    suspendprocess (0.02) // Wait 20 ms., enough time to do screen updates regarding to the background job, which calls this function
    dispatch_async(dispatch_get_main_queue())
    {
        var myFiledialog: NSOpenPanel = NSOpenPanel()
        var fileTypeArray: [String] = filetypelist.componentsSeparatedByString(",")

        myFiledialog.prompt = "Open"
        myFiledialog.worksWhenModal = true
        myFiledialog.allowsMultipleSelection = false
        myFiledialog.canChooseDirectories = false
        myFiledialog.resolvesAliases = true
        myFiledialog.title = windowTitle
        myFiledialog.message = message
        myFiledialog.allowedFileTypes = fileTypeArray

        let void = myFiledialog.runModal()

        var chosenfile = myFiledialog.URL // Pathname of the file

        if (chosenfile != nil)
        {
            path = chosenfile!.absoluteString!
        }
        finished = true
    }

    while not(finished)
    {
        suspendprocess (0.001) // Wait 1 ms., loop until main thread finished
    }

    return (path)
}

请注意, block 是异步调用的,这意味着您必须检查 block 是否已被处理并完成。所以我添加了一个 bool 变量“finsihed”,它显示了 block 到达其末尾的时间。没有它,您将得不到路径名,而只会得到一个空字符串。

如果您有兴趣,我也会发布我的保存文件对话框功能。如果是这样,请发表评论。

关于swift - 打开文件对话框在 Swift 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25473764/

相关文章:

IOS,从应用程序目录加载文件

ios - 为非续订订阅恢复未处理的 SKPaymentTransaction

ios - 在标签栏中滚动功能很奇怪

xcode - 调试文本消息以崩溃日志输出

arrays - 将文件名存储在数组 Swift 中

Swift FileManager - 没有这样的文件

swift - 为什么 Playgrounds 中的 View 和 ViewModel 之间的绑定(bind)不起作用?

ios - 单击前景上的推送通知横幅后如何触发操作

ios - 应用程序加载器显示旧图标

ios - 没有更多上下文,表达式类型不明确