2023-11-13|閱讀時間 ‧ 約 24 分鐘

[MacOS]SavePanel/OpenPanel

儲存檔案:

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")
}
}




分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.