【问题标题】:NSOpenPanel/NSSavePanel crashes in Swift 3NSOpenPanel/NSSavePanel 在 Swift 3 中崩溃
【发布时间】:2016-12-27 17:48:23
【问题描述】:

在 Swift 3/Xcode8.1/10.12.2 中,我尝试使用 NSOpenPanel 来获取包含以下代码的文本文件,由 DenBeke 编写

@IBAction func browseFile(sender: AnyObject) {

  let dialog = NSOpenPanel();

  dialog.title                   = "Choose a .txt file";
  dialog.allowedFileTypes        = ["txt"];

  if (dialog.runModal() == NSModalResponseOK)
  {
     let result = dialog.url // Pathname of the file

     if (result != nil) {
        let path = result!.path
        print("browseFile path: \(path)")
        //filename_field.stringValue = path
     }
  } else {
     // User clicked on "Cancel"
     return
  }
}

代码按预期打开一个打开的对话框,我可以选择一个文件。单击打开按钮会使应用程序崩溃。在控制台中我得到:

FI_TFloatingInputWindowController 对象 0x60800009c0c0 在释放时过度释放;打破 objc_overrelease_during_dealloc_error 进行调试

当我运行代码并打开对话框时,我在控制台中得到

[默认] [错误] 无法获取 URL 的容器:file:///Users/ruediheimlicher/Documents/LoggerdataDir/Messungen/,错误:错误域 = BRCloudDocsErrorDomain 代码 = 12“找不到应用程序库:'com.apple .Documents'" UserInfo={NSDescription=App library not found: 'com.apple.Documents'}

但这不会影响应用程序。

网上有更多关于 NSOpen 或 NSSave 对话框的示例,代码略有不同,但对于每个人,我都得到相同的结果:崩溃,控制台上出现完全相同的错误。

在我的代码尝试中是否有错误,甚至是一个适用于 Swift3/sierra 的示例?

【问题讨论】:

    标签: macos swift3 macos-sierra nsopenpanel nssavepanel


    【解决方案1】:

    授予您的应用访问用户在<Your Project> -> <Your Target> -> Capabilities -> App Sandbox -> File Access -> User Selected File 下选择的文件的权限。

    【讨论】:

    • 我的项目中没有这样的设置。
    • @heimi 我将答案调整为最新的 Xcode 版本。
    • 我现在得到的日志显示“无法获取 url 的所有者 UUID:”,其中 URL 指向我 6 年前最后一次以管理员身份登录的计算机上的文件。
    • heimi 你解决过这个问题吗?我也得到这个。它会在某个地方出现偏好文件吗?
    【解决方案2】:

    [默认] [错误] 无法获取 URL 的容器:file:///Users/ruediheimlicher/Documents/LoggerdataDir/Messungen/,错误:错误域 = BRCloudDocsErrorDomain 代码 = 12“找不到应用程序库:'com.apple .Documents'" UserInfo={NSDescription=App library not found: 'com.apple.Documents'}

    要删除此输出,您必须在 Finder 偏好设置中启用 iCloud Drive。

    • 打开查找器
    • 打开偏好设置
    • 选择第三个标签(侧边栏)
    • 检查 iCloud 驱动器

    然后重新运行您的应用程序

    【讨论】:

    • iCloudPreferences 用于代码 2 错误:Code=2 "Logged out - iCloud Drive is not configured" UserInfo={NSDescription=Logged out - iCloud Drive is not configured} 但上述错误是代码 12 App library not found,iCloudPreferences 无法解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多