2016-12-27 82 views
4

在斯威夫特3/Xcode8.1/10.12.2我试图用一个NSOpenPanel获得用下面的代码的文本文件,通过DenBekeNSOpenPanel/NSSavePanel崩溃斯威夫特3

书面
@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 object 0x60800009c0c0 overreleased while already deallocating; break on objc_overrelease_during_dealloc_error to debug

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

[default] [ERROR] Failed getting container for URL: file:///Users/ruediheimlicher/Documents/LoggerdataDir/Messungen/, error: Error Domain=BRCloudDocsErrorDomain Code=12 "App library not found: 'com.apple.Documents'" UserInfo={NSDescription=App library not found: 'com.apple.Documents'}

但这并不影响应用程序。

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

在我的代码尝试中是否存在错误,或者甚至是与Swift3/sierra一起使用的示例?

回答

5

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

User Selected File Permission

+0

我在我的项目中没有这样的设置。 – heimi

+0

@heimi我将我的答案调整为最新的Xcode版本。 – John

+0

非常好,那个伎俩。谢谢。 – heimi

2

[default] [ERROR] Failed getting container for URL: file:///Users/ruediheimlicher/Documents/LoggerdataDir/Messungen/, error: Error Domain=BRCloudDocsErrorDomain Code=12 "App library not found: 'com.apple.Documents'" UserInfo={NSDescription=App library not found: 'com.apple.Documents'}

要删除此输出,您必须启用iCloud的驱动器在您的Finder偏好。

  • 打开Finder
  • 打开使用偏好
  • 选择第三个选项卡(边栏)
  • 检查iCloud的驱动

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

+0

iCloud Drive已启用。 – heimi