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?
Give your app access to user selected files under
<Your Project> -> <Your Target> -> Capabilities -> App Sandbox -> File Access -> User Selected File
.