From a806f24a1a979b7d5edd28d2e083fdbe0ad84a4d Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Mon, 14 Nov 2016 01:37:13 +0100 Subject: [PATCH] Fix all compile errors --- AVRsack/ASApplication.swift | 56 +++++----- AVRsack/ASBuilder.swift | 36 +++--- AVRsack/ASFileTree.swift | 18 ++- AVRsack/ASHardware.swift | 8 +- AVRsack/ASPreferences.swift | 4 +- AVRsack/ASProjDoc.swift | 216 +++++++++++++++++------------------- 6 files changed, 170 insertions(+), 168 deletions(-) diff --git a/AVRsack/ASApplication.swift b/AVRsack/ASApplication.swift index 20b0795..f5b6e32 100644 --- a/AVRsack/ASApplication.swift +++ b/AVRsack/ASApplication.swift @@ -29,7 +29,7 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { let workSpace = NSWorkspace.shared() let userDefaults = UserDefaults.standard let appDefaultsURL = Bundle.main.url(forResource: "Defaults", withExtension: "plist")! - var appDefaults = NSDictionary(contentsOfURL: appDefaultsURL) as! [String: AnyObject] + var appDefaults = NSDictionary(contentsOf: appDefaultsURL) as! [String: AnyObject] // // Add dynamic app defaults // @@ -38,8 +38,8 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { } var sketchbooks = [NSString]() for doc in fileManager.urls(for: .documentDirectory, in: .userDomainMask) { - sketchbooks.append(doc.URLByAppendingPathComponent("Arduino").path!) - sketchbooks.append(doc.URLByAppendingPathComponent("AVRSack").path!) + sketchbooks.append(doc.appendingPathComponent("Arduino").path) + sketchbooks.append(doc.appendingPathComponent("AVRSack").path) } appDefaults["Sketchbooks"] = sketchbooks if fileManager.fileExists(atPath: "/usr/local/CrossPack-AVR") { @@ -82,8 +82,8 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { menu.removeAllItems() examples = [String]() if let arduinoURL = NSWorkspace.shared().urlForApplication(withBundleIdentifier: "cc.arduino.Arduino") { - let examplePath = arduinoURL.URLByAppendingPathComponent("Contents/Resources/Java/examples", isDirectory:true).path! - ASSketchBook.addSketches(menu, target: self, action: "openExample:", path: examplePath, sketches: &examples) + let examplePath = arduinoURL.appendingPathComponent("Contents/Resources/Java/examples", isDirectory:true).path + ASSketchBook.addSketches(menu: menu, target: self, action: #selector(ASApplication.openExample(item:)), path: examplePath, sketches: &examples) } case "Import Standard Library": menu.removeAllItems() @@ -108,7 +108,7 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { ASSerialWin.showWindowWithPort(port: port.title) } - func openTemplate(template: NSURL, fromReadOnly: Bool) { + func openTemplate(template: URL, fromReadOnly: Bool) { let editable : String if fromReadOnly { editable = "editable " @@ -116,31 +116,31 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { editable = "" } ASApplication.newProjectLocation(documentWindow: nil, - message: "Save \(editable)copy of project \(template.lastPathComponent!)") + message: "Save \(editable)copy of project \(template.lastPathComponent)") { (saveTo) -> Void in - let oldName = template.lastPathComponent! - let newName = saveTo.lastPathComponent! + let oldName = template.lastPathComponent + let newName = saveTo.lastPathComponent let fileManager = FileManager.default do { - try fileManager.copyItemAtURL(template, toURL: saveTo) - let contents = fileManager.enumeratorAtURL(saveTo, + try fileManager.copyItem(at: template, to: saveTo) + let contents = fileManager.enumerator(at: saveTo, includingPropertiesForKeys: [URLResourceKey.nameKey, URLResourceKey.pathKey], - options: .SkipsHiddenFiles, errorHandler: nil) - while let item = contents?.nextObject() as? NSURL { - let itemBase = item.URLByDeletingPathExtension?.lastPathComponent! + options: .skipsHiddenFiles, errorHandler: nil) + while let item = contents?.nextObject() as? URL { + let itemBase = item.deletingPathExtension().lastPathComponent if itemBase == oldName { - let newItem = item.URLByDeletingLastPathComponent!.URLByAppendingPathComponent( - newName).URLByAppendingPathExtension(item.pathExtension!) - try fileManager.moveItemAtURL(item, toURL: newItem) + let newItem = item.deletingLastPathComponent().appendingPathComponent( + newName).appendingPathExtension(item.pathExtension) + try fileManager.moveItem(at: item, to: newItem) } } } catch (_) { } - let sketch = ASSketchBook.findSketch(path: saveTo.path!) + let sketch = ASSketchBook.findSketch(path: saveTo.path) switch sketch { case .Sketch(_, let path): let doc = NSDocumentController.shared() - doc.openDocumentWithContentsOfURL(NSURL(fileURLWithPath: path), display: true) { (doc, alreadyOpen, error) -> Void in + doc.openDocument(withContentsOf: URL(fileURLWithPath: path), display: true) { (doc, alreadyOpen, error) -> Void in } default: break @@ -149,9 +149,9 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { } @IBAction func openSketch(item: NSMenuItem) { - let url = NSURL(fileURLWithPath: sketches[item.tag]) + let url = URL(fileURLWithPath: sketches[item.tag]) let doc = NSDocumentController.shared() - doc.openDocumentWithContentsOfURL(url, display: true) { (doc, alreadyOpen, error) -> Void in + doc.openDocument(withContentsOf: url, display: true) { (doc, alreadyOpen, error) -> Void in } } @@ -164,23 +164,23 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { ASApplication.newProjectLocation(documentWindow: nil, message: "Create Project") { (saveTo) -> Void in - let fileManager = FileManager.defaultManager() + let fileManager = FileManager.default do { - try fileManager.createDirectoryAtURL(saveTo, withIntermediateDirectories:false, attributes:nil) - let proj = saveTo.appendingPathComponent(saveTo.lastPathComponent!+".avrsackproj") + try fileManager.createDirectory(at: saveTo, withIntermediateDirectories:false, attributes:nil) + let proj = saveTo.appendingPathComponent(saveTo.lastPathComponent+".avrsackproj") let docController = NSDocumentController.shared() if let doc = try docController.openUntitledDocumentAndDisplay(true) as? ASProjDoc { doc.fileURL = proj doc.updateProjectURL() - doc.createFileAtURL(url: saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent!+".ino")) - try doc.writeToURL(proj, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil) + doc.createFileAtURL(url: saveTo.appendingPathComponent(saveTo.lastPathComponent+".ino")) + try doc.write(to: proj, ofType: "Project", for: .saveAsOperation, originalContentsURL: nil) } } catch _ { } } } - class func newProjectLocation(documentWindow: NSWindow?, message: String, completion: (NSURL) -> ()) { + class func newProjectLocation(documentWindow: NSWindow?, message: String, completion: (URL) -> ()) { let savePanel = NSSavePanel() savePanel.allowedFileTypes = [kUTTypeFolder as String] savePanel.message = message @@ -219,7 +219,7 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { default: abort() } - NSWorkspace.sharedWorkspace().openURL(NSURL(string: helpString)!) + NSWorkspace.shared().open(URL(string: helpString)!) } } diff --git a/AVRsack/ASBuilder.swift b/AVRsack/ASBuilder.swift index 4e6adf4..82917bf 100644 --- a/AVRsack/ASBuilder.swift +++ b/AVRsack/ASBuilder.swift @@ -9,7 +9,7 @@ import Foundation class ASBuilder { - var dir = NSURL() + var dir = URL(fileURLWithPath: "/") var task : Task? var continuation: (()->())? var termination : AnyObject? @@ -34,8 +34,8 @@ class ASBuilder { NotificationCenter.default.removeObserver(termination!) } - func setProjectURL(url: NSURL) { - dir = url.URLByDeletingLastPathComponent!.URLByStandardizingPath! + func setProjectURL(url: URL) { + dir = url.deletingLastPathComponent().standardizedFileURL } func stop() { @@ -45,7 +45,7 @@ class ASBuilder { func cleanProject() { do { - try FileManager.default.removeItem(at: dir.appendingPathComponent("build")!) + try FileManager.default.removeItem(at: dir.appendingPathComponent("build")) } catch _ { } } @@ -53,7 +53,7 @@ class ASBuilder { func buildProject(board: String, files: ASFileTree) { let toolChain = (NSApplication.shared().delegate as! ASApplication).preferences.toolchainPath task = Task() - task!.currentDirectoryPath = dir.path! + task!.currentDirectoryPath = dir.path task!.launchPath = Bundle.main.path(forResource: "BuildProject", ofType: "")! let fileManager = FileManager.default @@ -81,7 +81,7 @@ class ASBuilder { return } args.append("toolchain="+toolChain) - args.append("project="+dir.lastPathComponent!) + args.append("project="+dir.lastPathComponent) args.append("board="+board) args.append("mcu="+boardProp["build.mcu"]!) args.append("f_cpu="+boardProp["build.f_cpu"]!) @@ -112,7 +112,7 @@ class ASBuilder { let portPath = ASSerial.fileName(forPort: port) let toolChain = (NSApplication.shared().delegate as! ASApplication).preferences.toolchainPath task = Task() - task!.currentDirectoryPath = dir.path! + task!.currentDirectoryPath = dir.path task!.launchPath = toolChain+"/bin/avrdude" let fileManager = FileManager.default @@ -127,8 +127,8 @@ class ASBuilder { } else { ASSerialWin.portNeededForUpload(port: port) let logURL = dir.appendingPathComponent("build/upload.log") - fileManager.createFileAtPath(logURL.path, contents: NSData(), attributes: nil) - logOut = FileHandle(forWritingAtPath: logURL.path!)! + fileManager.createFile(atPath: logURL.path, contents: Data(), attributes: nil) + logOut = FileHandle(forWritingAtPath: logURL.path)! task!.standardOutput = logOut task!.standardError = logOut } @@ -147,13 +147,13 @@ class ASBuilder { var args = Array(repeating: "-v", count: verbosity) args += [ "-C", toolChain+"/etc/avrdude.conf", - "-p", boardProp["build.mcu"]!, "-c", proto!, "-P", portPath] + "-p", boardProp["build.mcu"]!, "-c", proto!, "-P", portPath!] switch mode { case .Upload: if hasBootloader { args += ["-D"] } - args += ["-U", "flash:w:build/"+board+"/"+dir.lastPathComponent!+".hex:i"] + args += ["-U", "flash:w:build/"+board+"/"+dir.lastPathComponent+".hex:i"] continuation = { DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: { ASSerialWin.portAvailableAfterUpload(port: port) @@ -183,7 +183,7 @@ class ASBuilder { } if needPhase2 { let task2 = Task() - task2.currentDirectoryPath = dir.path! + task2.currentDirectoryPath = dir.path task2.launchPath = toolChain+"/bin/avrdude" task2.arguments = loaderArgs task2.standardOutput = logOut @@ -220,7 +220,7 @@ class ASBuilder { sleep(1) for retry in 0 ..< 40 { usleep(250000) - if (fileManager.fileExistsAtPath(portPath)) { + if (fileManager.fileExists(atPath: portPath!)) { if verbosity > 0 { logOut.write("Found port \(port) after \(retry) attempts.\n".data(using: String.Encoding.utf8, allowLossyConversion: true)!) } @@ -243,21 +243,21 @@ class ASBuilder { func disassembleProject(board: String) { let toolChain = (NSApplication.shared().delegate as! ASApplication).preferences.toolchainPath task = Task() - task!.currentDirectoryPath = dir.path! + task!.currentDirectoryPath = dir.path task!.launchPath = toolChain+"/bin/avr-objdump" let fileManager = FileManager.default let logURL = dir.appendingPathComponent("build/disasm.log") - fileManager.createFileAtPath(logURL.path!, contents: NSData(), attributes: nil) - let logOut = FileHandle(forWritingAtPath: logURL.path!)! + fileManager.createFile(atPath: logURL.path, contents: Data(), attributes: nil) + let logOut = FileHandle(forWritingAtPath: logURL.path)! task!.standardOutput = logOut task!.standardError = logOut let showSource = UserDefaults.standard.bool(forKey: "ShowSourceInDisassembly") var args = showSource ? ["-S"] : [] - args += ["-d", "build/"+board+"/"+dir.lastPathComponent!+".elf"] + args += ["-d", "build/"+board+"/"+dir.lastPathComponent+".elf"] let cmdLine = task!.launchPath!+" "+(args as NSArray).componentsJoined(by: " ")+"\n" - logOut.writeData(cmdLine.dataUsingEncoding(String.Encoding.utf8, allowLossyConversion: true)!) + logOut.write(cmdLine.data(using: String.Encoding.utf8, allowLossyConversion: true)!) task!.arguments = args; task!.launch() } diff --git a/AVRsack/ASFileTree.swift b/AVRsack/ASFileTree.swift index 87ec71e..115895e 100644 --- a/AVRsack/ASFileTree.swift +++ b/AVRsack/ASFileTree.swift @@ -211,7 +211,13 @@ class ASFileItem : ASFileNode { } else { url = URL(fileURLWithPath: path as String, relativeTo: rootURL).standardizedFileURL } - if try! !url.checkResourceIsReachable() { + var fileExists = false + do { + fileExists = try url.checkResourceIsReachable() + } catch { + fileExists = false + } + if !fileExists { // // When projects get moved, .ino files get renamed but that fact is not // yet reflected in the project file. @@ -219,7 +225,7 @@ class ASFileItem : ASFileNode { let urlDir = url.deletingLastPathComponent() let newName = rootURL.appendingPathExtension(url.pathExtension).lastPathComponent let altURL = urlDir.appendingPathComponent(newName) - if try! altURL.checkResourceIsReachable() { + if let altExists = try? altURL.checkResourceIsReachable(), altExists { url = altURL } } @@ -263,11 +269,15 @@ class ASFileItem : ASFileNode { } override func exists() -> Bool { - return try! url.checkResourceIsReachable() + do { + return try url.checkResourceIsReachable() + } catch { + return false + } } override func modDate() -> Date? { - let values = try? url.resourceValues(forKeys: [URLResourceKey.contentModificationDateKey]) + let values = try? url.resourceValues(forKeys: [.contentModificationDateKey]) return values?.contentModificationDate } diff --git a/AVRsack/ASHardware.swift b/AVRsack/ASHardware.swift index 96d0cfc..068ac98 100644 --- a/AVRsack/ASHardware.swift +++ b/AVRsack/ASHardware.swift @@ -177,24 +177,24 @@ class ASLibraries : NSObject { } func addStandardLibrariesToMenu(menu: NSMenu) { for (index,lib) in standardLib.enumerated() { - let menuItem = menu.addItem(withTitle: (lib as NSString).lastPathComponent, action: #selector(ASLibraries.importStandardLibrary(_:)), keyEquivalent: "") + let menuItem = menu.addItem(withTitle: (lib as NSString).lastPathComponent, action: #selector(ASLibraries.importStandardLibrary(menuItem:)), keyEquivalent: "") menuItem.target = self menuItem.tag = index } } func addContribLibrariesToMenu(menu: NSMenu) { for (index,lib) in contribLib.enumerated() { - let menuItem = menu.addItem(withTitle: (lib as NSString).lastPathComponent, action: #selector(ASLibraries.importContribLibrary(_:)), keyEquivalent: "") + let menuItem = menu.addItem(withTitle: (lib as NSString).lastPathComponent, action: #selector(ASLibraries.importContribLibrary(menuItem:)), keyEquivalent: "") menuItem.target = self menuItem.tag = index } } - @IBAction func importStandardLibrary(_ menuItem: AnyObject) { + @IBAction func importStandardLibrary(menuItem: AnyObject) { if let tag = (menuItem as? NSMenuItem)?.tag { NSApplication.shared().sendAction(#selector(ASProjDoc.importLibrary(_:)), to: nil, from: standardLib[tag]) } } - @IBAction func importContribLibrary(_ menuItem: AnyObject) { + @IBAction func importContribLibrary(menuItem: AnyObject) { if let tag = (menuItem as? NSMenuItem)?.tag { NSApplication.shared().sendAction(#selector(ASProjDoc.importLibrary(_:)), to: nil, from: contribLib[tag]) } diff --git a/AVRsack/ASPreferences.swift b/AVRsack/ASPreferences.swift index f28c76f..e24e34f 100644 --- a/AVRsack/ASPreferences.swift +++ b/AVRsack/ASPreferences.swift @@ -87,7 +87,7 @@ class ASPreferences: NSWindowController, NSOpenSavePanelDelegate { } func panel(_ sender: AnyObject, shouldEnable url: URL) -> Bool { - let gccPath = url.URLByAppendingPathComponent("bin/avr-gcc") - return FileManager.defaultManager().fileExistsAtPath(gccPath.path!) + let gccPath = url.appendingPathComponent("bin/avr-gcc") + return FileManager.default.fileExists(atPath: gccPath.path) } } diff --git a/AVRsack/ASProjDoc.swift b/AVRsack/ASProjDoc.swift index d95553f..e72558a 100644 --- a/AVRsack/ASProjDoc.swift +++ b/AVRsack/ASProjDoc.swift @@ -48,11 +48,11 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa dynamic var port : String = "" var recentBoards = [String]() var recentProgrammers = [String]() - var logModified = NSDate.distantPast + var logModified = Date.distantPast var logSize = 0 var updateLogTimer : Timer? var printingDone : () -> () = {} - var printModDate : NSDate? + var printModDate : Date? var printRevision : String? var printShowPanel = false var jumpingToIssue = false @@ -130,7 +130,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa outline.setDraggingSourceOperationMask(NSDragOperation.every, forLocal: true) outline.setDraggingSourceOperationMask([], forLocal: false) - outline.setDataSource(files) + outline.dataSource = files files.apply() { node in if let group = node as? ASFileGroup { if group.expanded { @@ -160,13 +160,13 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa func saveCurEditor() { if let file = (mainEditor as? ASFileItem) { do { - try editor.string().writeToURL(file.url, atomically: true, encoding: String.Encoding.utf8) + try editor.string().write(to: file.url, atomically: true, encoding: String.Encoding.utf8) } catch _ { } } } - - override func dataOfType(typeName: String) throws -> NSData { + + override func data(ofType typeName: String) throws -> Data { let data = [kVersionKey: kCurVersion, kThemeKey: ACEThemeNames.name(for: currentTheme), kFontSizeKey: fontSize, @@ -177,7 +177,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa kRecentBoardsKey: recentBoards, kRecentProgrammersKey: recentProgrammers ] - return try PropertyListSerialization.dataWithPropertyList(data, format:.XMLFormat_v1_0, options:0) + return try PropertyListSerialization.data(fromPropertyList: data, format:.xml, options:0) } func updateProjectURL() { @@ -185,44 +185,44 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa builder.setProjectURL(url: fileURL!) } - func importProject(url: NSURL) throws { - let existingProject = url.appendingPathComponent(url.lastPathComponent!+".avrsackproj") - if existingProject.checkResourceIsReachableAndReturnError(nil) { + func importProject(url: URL) throws { + let existingProject = url.appendingPathComponent(url.lastPathComponent+".avrsackproj") + if let hasProject = try? existingProject.checkResourceIsReachable(), hasProject { fileURL = existingProject - try readFromURL(url: existingProject, ofType:"Project") + try read(from: existingProject, ofType:"Project") return } let filesInProject = - (try FileManagerefaultManager().contentsOfDirectoryAtURL(url, includingPropertiesForKeys: nil, - options: .SkipsHiddenFiles)) + (try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, + options: .skipsHiddenFiles)) updateProjectURL() for file in filesInProject { - files.addFileURL(file) + files.addFileURL(url: file) } } - override func readFromURL(url: NSURL, ofType typeName: String) throws { + override func read(from url: URL, ofType typeName: String) throws { if typeName == "Arduino Source File" { - let projectURL = url.URLByDeletingPathExtension!.URLByAppendingPathExtension("avrsackproj") - try importProject(url: url.deletingLastPathComponent!) + let projectURL = url.deletingPathExtension().appendingPathExtension("avrsackproj") + try importProject(url: url.deletingLastPathComponent()) fileURL = projectURL - try writeToURL(projectURL, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil) + try write(to: projectURL, ofType: "Project", for: .saveAsOperation, originalContentsURL: nil) } else { fileURL = url - try super.readFromURL(url, ofType: typeName) + try super.read(from: url, ofType: typeName) } } - override func readFromData(data: NSData, ofType typeName: String) throws { + override func read(from data: Data, ofType typeName: String) throws { if typeName != ("Project" as String) { throw NSError(domain: "AVRSack", code: 0, userInfo: nil) } updateProjectURL() let projectData = - (try PropertyListSerialization.propertyListWithData(data, options:[], format:nil)) as! NSDictionary + (try PropertyListSerialization.propertyList(from: data, options:[], format:nil)) as! NSDictionary let projectVersion = projectData[kVersionKey] as! Double assert(projectVersion <= floor(kCurVersion+1.0), "Project version too new for this app") if let themeName = projectData[kThemeKey] as? String { - if let themeId = ACEView.themeIdByName(themeName) { + if let themeId = ACEView.themeIdByName(themeName: themeName) { currentTheme = themeId } } @@ -240,32 +240,25 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa override func duplicate(_ sender: AnyObject?) { let app = NSApplication.shared().delegate as! ASApplication - app.openTemplate(fileURL!.URLByDeletingLastPathComponent!, fromReadOnly:false) + app.openTemplate(template: fileURL!.deletingLastPathComponent(), fromReadOnly:false) } func updateLog(_: AnyObject?) { if let logNode = mainEditor as? ASLogNode { - let url = fileURL?.URLByDeletingLastPathComponent?.URLByAppendingPathComponent(logNode.path) - if url == nil { - return - } - var modified : AnyObject? - var size : AnyObject? - do { - try url!.getResourceValue(&modified, forKey:NSURLAttributeModificationDateKey) - try url!.getResourceValue(&size, forKey:NSURLFileSizeKey) - } catch (_) { - return - } - - if (modified as! NSDate).compare(logModified) == .orderedDescending || (size as! Int) != logSize { - var enc : UInt = 0 - let newText = try? NSString(contentsOfURL:url!, usedEncoding:&enc) - editor.setString((newText as? String) ?? "") - editor.gotoLine(1000000000, column: 0, animated: true) - logModified = modified as! NSDate - logSize = size as! Int - currentIssueLine = -1 + guard let fileURL = fileURL else { return } + let url = fileURL.deletingLastPathComponent().appendingPathComponent(logNode.path) + if let values = try? url.resourceValues(forKeys: [.attributeModificationDateKey, .fileSizeKey]) { + if values.attributeModificationDate!.compare(logModified) == .orderedDescending + || values.fileSize! != logSize + { + var enc : String.Encoding = .utf8 + let newText = try? String(contentsOf: url, usedEncoding:&enc) + editor.setString(newText ?? "") + editor.gotoLine(1000000000, column: 0, animated: true) + logModified = values.attributeModificationDate! + logSize = values.fileSize! + currentIssueLine = -1 + } } } } @@ -274,9 +267,9 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa saveCurEditor() } if let file = (selection as? ASFileItem) { - var enc : UInt = 0 - let contents = try? NSString(contentsOfURL:file.url, usedEncoding:&enc) - editor.setString(contents as? String ?? "") + var enc : String.Encoding = .utf8 + let contents = try? String(contentsOf:file.url, usedEncoding:&enc) + editor.setString(contents ?? "") editor.setMode(file.type.aceMode) editor.alphaValue = 1.0 mainEditor = selection @@ -293,13 +286,13 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa } } func selectNodeInOutline(selection: ASFileNode) { - let selectedIndexes = NSIndexSet(index: outline.row(forItem: selection)) + let selectedIndexes = IndexSet(integer: outline.row(forItem: selection)) outline.selectRowIndexes(selectedIndexes, byExtendingSelection: false) } func selectedFiles() -> [ASFileItem] { var selection = [ASFileItem]() - outline.selectedRowIndexes.enumerateIndexesUsingBlock() { (index, stop) in - if let file = self.outline.itemAtRow(index) as? ASFileItem { + for index in outline.selectedRowIndexes { + if let file = self.outline.item(atRow: index) as? ASFileItem { selection.append(file) } } @@ -308,22 +301,20 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa // MARK: Printing - override func print(withSettings printSettings: [String : AnyObject], showPrintPanel: Bool, delegate: AnyObject?, didPrint didPrintSelector: Selector?, contextInfo: UnsafeMutablePointer) { + override func print(withSettings printSettings: [String : AnyObject], showPrintPanel: Bool, delegate: AnyObject?, didPrint didPrintSelector: Selector?, contextInfo: UnsafeMutablePointer?) { printingDone = { () -> () in InvokeCallback(delegate, didPrintSelector, contextInfo); } + printModDate = nil if let logNode = mainEditor as? ASLogNode { - printModDate = nil - if let url = fileURL?.URLByDeletingLastPathComponent?.URLByAppendingPathComponent(logNode.path) { - do { - var modified : AnyObject? - try url.getResourceValue(&modified, forKey:NSURLAttributeModificationDateKey) - printModDate = modified as? NSDate - } catch (_) { - } + if let url = fileURL?.deletingLastPathComponent().appendingPathComponent(logNode.path), + let values = try? url.resourceValues(forKeys: [.attributeModificationDateKey]) + { + printModDate = values.attributeModificationDate } - } else { + } + if printModDate == nil { printModDate = mainEditor?.modDate() } printRevision = mainEditor?.revision() @@ -454,10 +445,10 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm" - let modDateStr = dateFormatter.stringFromDate(modDate) - let modDateSize = modDateStr.sizeWithAttributes(footAttr) + let modDateStr = dateFormatter.string(from: modDate) + let modDateSize = modDateStr.size(withAttributes: footAttr) footAt.x = rect.origin.x+rect.size.width-modDateSize.width-kXOffset - modDateStr.drawAtPoint(footAt, withAttributes:footAttr) + modDateStr.draw(at: footAt, withAttributes:footAttr) } } @@ -489,13 +480,13 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa } func outlineView(_ outlineView: NSOutlineView, willDisplayCell cell: AnyObject, for tableColumn: NSTableColumn?, item: AnyObject) { if let textCell = cell as? NSTextFieldCell { - textCell.textColor = NSColor.blackColor + textCell.textColor = NSColor.black if item === files.root || item === files.buildLog || item === files.uploadLog || item === files.disassembly { textCell.font = NSFont.boldSystemFont(ofSize: 13.0) } else { textCell.font = NSFont.systemFont(ofSize: 13.0) if !(item as! ASFileNode).exists() { - textCell.textColor = NSColor.redColor + textCell.textColor = NSColor.red } } } @@ -510,7 +501,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa var name : String var ref : String if selection.count == 1 { - name = "file “\(selection[0].url.lastPathComponent!)”" + name = "file “\(selection[0].url.lastPathComponent)”" ref = "reference to it" } else { name = "\(selection.count) selected files" @@ -527,7 +518,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa alert.beginSheetModal(for: outline.window!) { (response) in if response != NSAlertThirdButtonReturn { if response == NSAlertFirstButtonReturn { - NSWorkspace.sharedWorkspace().recycleURLs(selection.map {$0.url}, completionHandler:nil) + NSWorkspace.shared().recycle(selection.map {$0.url}, completionHandler:nil) } self.files.apply { (node) in if let group = node as? ASFileGroup { @@ -569,18 +560,19 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa } func panel(_ panel:AnyObject, shouldEnable url:URL) -> Bool { - var shouldEnable = true - var resourceID : AnyObject? - guard ((try? url.getResourceValue(&resourceID, forKey:NSURLFileResourceIdentifierKey)) != nil) else { - return true; + guard let values = try? url.resourceValues(forKeys: [.fileResourceIdentifierKey]), + let resourceID = values.fileResourceIdentifier + else { + return true } + var shouldEnable = true files.apply {(node) in - if let file = node as? ASFileItem { - var thisID : AnyObject? - if (try? file.url.getResourceValue(&thisID, forKey:URLResourceKey.fileResourceIdentifierKey)) != nil { - if thisID != nil && resourceID!.isEqual(thisID!) { - shouldEnable = false - } + if let file = node as? ASFileItem, + let values = try? file.url.resourceValues(forKeys: [.fileResourceIdentifierKey]), + let thisID = values.fileResourceIdentifier + { + if resourceID.isEqual(thisID) { + shouldEnable = false } } } @@ -591,7 +583,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa return selectedFiles().count > 0 } - func createFileAtURL(url:NSURL) { + func createFileAtURL(url: URL) { let type = ASFileType.guessForURL(url: url) var firstPfx = "" var prefix = "" @@ -623,13 +615,13 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa let dateFmt = DateFormatter() dateFmt.dateFormat = "yyyy-MM-dd" header = firstPfx + "\n" + - prefix + " Project: " + fileURL!.URLByDeletingLastPathComponent!.lastPathComponent! + "\n" + - prefix + " File: " + url.lastPathComponent! + "\n" + - prefix + " Created: " + dateFmt.stringFromDate(NSDate()) + "\n" + + prefix + " Project: " + fileURL!.deletingLastPathComponent().lastPathComponent + "\n" + + prefix + " File: " + url.lastPathComponent + "\n" + + prefix + " Created: " + dateFmt.string(from: Date()) + "\n" + lastPfx + "\n\n" } do { - try header.writeToURL(url, atomically: true, encoding: String.Encoding.utf8) + try header.write(to: url, atomically: true, encoding: String.Encoding.utf8) } catch _ { } files.addFileURL(url: url) @@ -691,18 +683,18 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa override func validateUserInterfaceItem(_ anItem: NSValidatedUserInterfaceItem) -> Bool { if let menuItem = anItem as? NSMenuItem { - if menuItem.action == "changeTheme:" { + if menuItem.action == #selector(ASProjDoc.changeTheme(item:)) { menuItem.state = (UInt(menuItem.tag) == currentTheme.rawValue ? NSOnState : NSOffState) return true - } else if menuItem.action == "changeKeyboardHandler:" { + } else if menuItem.action == #selector(ASProjDoc.changeKeyboardHandler(item:)) { menuItem.state = (menuItem.tag == Int(keyboardHandler.rawValue) ? NSOnState : NSOffState) return true - } else if menuItem.action == "serialConnect:" { + } else if menuItem.action == #selector(ASProjDoc.serialConnect(sender:)) { menuItem.title = port return true - } else if menuItem.action == "importStandardLibrary:" || - menuItem.action == "importContribLibrary:" + } else if menuItem.action == #selector(ASLibraries.importStandardLibrary(menuItem:)) || + menuItem.action == #selector(ASLibraries.importContribLibrary(menuItem:)) { return mainEditor is ASFileItem } @@ -729,49 +721,49 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa if editors.views(in: .bottom).count == 0 { editors.addView(auxEdit, in: .bottom) - let url = fileURL?.URLByDeletingLastPathComponent?.URLByAppendingPathComponent(files.buildLog.path) + let url = fileURL?.deletingLastPathComponent().appendingPathComponent(files.buildLog.path) if url == nil { return } - var enc : UInt = 0 - let contents = try? NSString(contentsOfURL:url!, usedEncoding:&enc) - auxEdit.setString(contents as? String ?? "") + var enc : String.Encoding = .utf8 + let contents = try? String(contentsOf:url!, usedEncoding:&enc) + auxEdit.setString(contents ?? "") editor.setMode(.text) editor.alphaValue = 1.0 } - let buildLog = auxEdit.string().componentsSeparatedByString("\n") + let buildLog = auxEdit.string().components(separatedBy: "\n") let issueRe = try! NSRegularExpression(pattern: "(\\S+?):(\\d+):.*", options: []) currentIssueLine += direction while currentIssueLine > -1 && currentIssueLine < buildLog.count { let line = buildLog[currentIssueLine] let range = NSMakeRange(0, line.utf16.count) - if let match = issueRe.firstMatchInString(line, options:.Anchored, range:range) { - let file = match.rangeAtIndex(1) - let lineTxt = match.rangeAtIndex(2) + if let match = issueRe.firstMatch(in: line, options:.anchored, range:range) { + let file = match.rangeAt(1) + let lineTxt = match.rangeAt(2) let nsline = line as NSString - let lineNo = Int(nsline.substringWithRange(lineTxt))! - let fileName = nsline.substringWithRange(file) as NSString - let fileURL : NSURL + let lineNo = Int(nsline.substring(with: lineTxt))! + let fileName = nsline.substring(with: file) as NSString + let fileURL : URL if fileName.hasPrefix("../../") { - fileURL = files.dir.URLByAppendingPathComponent(fileName.substringFromIndex(6)) + fileURL = files.dir.appendingPathComponent(fileName.substring(from: 6)) } else { - fileURL = NSURL(fileURLWithPath:fileName as String).URLByStandardizingPath! + fileURL = URL(fileURLWithPath:fileName as String).standardizedFileURL } jumpingToIssue = true - var resourceID : AnyObject? - if (try? fileURL.getResourceValue(&resourceID, forKey:URLResourceKey.fileResourceIdentifierKey)) != nil && resourceID != nil { + if let values = try? fileURL.resourceValues(forKeys: [.fileResourceIdentifierKey]), + let resourceID = values.fileResourceIdentifier + { files.apply {(node) in - if let file = node as? ASFileItem { - var thisID : AnyObject? - if (try? file.url.getResourceValue(&thisID, forKey:URLResourceKey.fileResourceIdentifierKey)) != nil { - if thisID != nil && resourceID!.isEqual(thisID!) { - self.selectNodeInOutline(selection: node) - self.editor.gotoLine(lineNo, column:0, animated:true) - } - } + if let file = node as? ASFileItem, + let values = try? file.url.resourceValues(forKeys: [.fileResourceIdentifierKey]), + let thisID = values.fileResourceIdentifier, + resourceID.isEqual(thisID) + { + self.selectNodeInOutline(selection: node) + self.editor.gotoLine(lineNo, column:0, animated:true) } } } @@ -809,11 +801,11 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa switch menu.title { case "Boards": ASHardware.instance().buildBoardsMenu(menu: menu, recentBoards: recentBoards, - target: self, selector: "selectBoard:") + target: self, selector: #selector(ASProjDoc.selectBoard(item:))) boardTool.setTitle(selectedBoard) case "Programmers": ASHardware.instance().buildProgrammersMenu(menu: menu, recentProgrammers: recentProgrammers, - target: self, selector: "selectProgrammer:") + target: self, selector: #selector(ASProjDoc.selectProgrammer(item:))) progTool.setTitle(selectedProgrammer) default: break