在斯威夫特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一起使用的示例?
我在我的項目中沒有這樣的設置。 – heimi
@heimi我將我的答案調整爲最新的Xcode版本。 – John
非常好,那個伎倆。謝謝。 – heimi