[MacOS][swift]SavePanel/OpenPanel

小黑
發佈於APP
2023/11/13閱讀時間約 2 分鐘

儲存檔案:

let savePanel = NSSavePanel()
savePanel.canCreateDirectories = true
savePanel.showsTagField = false
savePanel.nameFieldStringValue = "localFile.txt"
savePanel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
savePanel.begin { (result) in
if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
do{
try self.writeTextView.string.write(to: savePanel.url!, atomically: true, encoding: .utf8)
}
catch{
NSLog("save Error")
}
}
}


開啟檔案:

let readPanel = NSOpenPanel()
readPanel.title = "Choose a .txt file";
readPanel.showsResizeIndicator = true;
readPanel.showsHiddenFiles = false;
readPanel.canChooseDirectories = true;
readPanel.canCreateDirectories = true;
readPanel.allowsMultipleSelection = false;
readPanel.allowedFileTypes = ["txt"];
if (readPanel.runModal() == NSApplication.ModalResponse.OK) {
//reading

do {
readTextView.string = try String(contentsOf: readPanel.url!, encoding: .utf8)
}
catch {
NSLog("read Error")
}
}




7會員
78內容數
嗨,我是一名程式設計師,會在這分享開發與學習紀錄。
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容