NSOpenPanel/NSSavePanel crashes in Swift 3
Asked Answered
L

2

5

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?

Larainelarboard answered 27/12, 2016 at 17:48 Comment(0)
S
9

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

Suziesuzuki answered 6/1, 2017 at 13:36 Comment(4)
I have no such setting in my project.Larainelarboard
@Larainelarboard I adjusted my answer to the latest Xcode version.Suziesuzuki
I now get logs with 'Faild to get owner UUID for url:' with URL's pointing to a file on a computer I was last logged in as an administrator 6 years before.Larainelarboard
heimi did you ever resolve this? I am getting this too. Could it be coming a preference file somewhere?Sharika
R
3

[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

R answered 19/2, 2017 at 10:15 Comment(1)
iCloudPreferences are for a Code 2 error: Code=2 "Logged out - iCloud Drive is not configured" UserInfo={NSDescription=Logged out - iCloud Drive is not configured} but the above error is a Code 12 App library not found which iCloudPreferences doesn't resolve.Mathur

© 2022 - 2024 — McMap. All rights reserved.