Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOpenPanel/NSSavePanel crashes in Swift 3

In Swift 3/Xcode8.1/10.12.2 I am trying to use a NSOpenPanel to get a text file with the following code, written by 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
  }
}

The code opens a open dialog as expected and I can choose a file. Clicking the open button crashes the app. In the console I get:

FI_TFloatingInputWindowController object 0x60800009c0c0 overreleased while already deallocating; break on objc_overrelease_during_dealloc_error to debug

When I run the code and open the dialog, in the console I get

[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'}

but this does not affect the app.

There are more examples for NSOpen or NSSave dialogs on the net with slightly different code, but with everyone I get the same result: Crash, and the exact same error on the console.

Is there a mistake in my code attempts, or even an example that works with Swift3/sierra?

like image 503
heimi Avatar asked Dec 27 '16 17:12

heimi


2 Answers

Give your app access to user selected files under <Your Project> -> <Your Target> -> Capabilities -> App Sandbox -> File Access -> User Selected File.

User Selected File Permission

like image 168
John Avatar answered Nov 04 '22 20:11

John


[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'}

To remove this output you have to enable iCloud Drive in your Finder preferences.

  • Open finder
  • Open prefences
  • Select third tab (Sidebar)
  • Check iCloud Drive

Then rerun your application

like image 3
Daniel Sumara Avatar answered Nov 04 '22 20:11

Daniel Sumara