儲存檔案:
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")
}
}