From 8d7e8c8c10408dd0031fe1adc668769f33607a9f Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Mon, 16 Nov 2015 02:56:33 +0100 Subject: [PATCH] Modernize to Swift 2.0 --- AVRsack.xcodeproj/project.pbxproj | 1 + AVRsack/ACEViewExt.swift | 4 +- AVRsack/ASApplication.swift | 83 ++++++------- AVRsack/ASBuilder.swift | 17 +-- AVRsack/ASFileTree.swift | 34 +++--- AVRsack/ASHardware.swift | 50 ++++---- AVRsack/ASProjDoc.swift | 196 ++++++++++++++++-------------- AVRsack/ASSerial.h | 2 +- AVRsack/ASSerial.mm | 2 +- AVRsack/ASSerialWin.swift | 21 ++-- AVRsack/ASSketchBook.swift | 20 +-- 11 files changed, 225 insertions(+), 205 deletions(-) diff --git a/AVRsack.xcodeproj/project.pbxproj b/AVRsack.xcodeproj/project.pbxproj index 12558a3..015e7d4 100644 --- a/AVRsack.xcodeproj/project.pbxproj +++ b/AVRsack.xcodeproj/project.pbxproj @@ -316,6 +316,7 @@ 9501D7F41A17025C0034C530 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftMigration = 0710; LastSwiftUpdateCheck = 0700; LastUpgradeCheck = 0630; ORGANIZATIONNAME = "Aere Perennius"; diff --git a/AVRsack/ACEViewExt.swift b/AVRsack/ACEViewExt.swift index 0992a83..807b133 100644 --- a/AVRsack/ACEViewExt.swift +++ b/AVRsack/ACEViewExt.swift @@ -10,7 +10,7 @@ import Foundation extension ACEView { class func themeIdByName(themeName: String) -> ACETheme? { - for (themeIdx, theme) in enumerate(ACEThemeNames.themeNames() as! [String]) { + for (themeIdx, theme) in (ACEThemeNames.themeNames() as! [String]).enumerate() { if themeName == theme { return ACETheme(rawValue: UInt(themeIdx)) } @@ -19,7 +19,7 @@ extension ACEView { } class func handlerIdByName(handlerName: String) -> ACEKeyboardHandler? { - for (handlerIdx, handler) in enumerate(ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as! [String]) { + for (handlerIdx, handler) in (ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as! [String]).enumerate() { if handlerName == handler { return ACEKeyboardHandler(rawValue: UInt(handlerIdx))! } diff --git a/AVRsack/ASApplication.swift b/AVRsack/ASApplication.swift index c463485..40948b9 100644 --- a/AVRsack/ASApplication.swift +++ b/AVRsack/ASApplication.swift @@ -18,11 +18,7 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { var examples = [String]() func hasDocument() -> Bool { - if let doc = NSDocumentController.sharedDocumentController().currentDocument as? NSDocument { - return true - } else { - return false - } + return NSDocumentController.sharedDocumentController().currentDocument != nil } func applicationWillFinishLaunching(notification: NSNotification) { @@ -56,12 +52,12 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { } func applicationDidFinishLaunching(aNotification: NSNotification) { themeMenu.removeAllItems() - for (index, theme) in enumerate(ACEThemeNames.humanThemeNames() as! [String]) { + for (index, theme) in (ACEThemeNames.humanThemeNames() as! [String]).enumerate() { let menuItem = themeMenu.addItemWithTitle(theme, action: "changeTheme:", keyEquivalent: "")! menuItem.tag = index } keyboardMenu.removeAllItems() - for (index, theme) in enumerate(ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as! [String]) { + for (index, theme) in (ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as! [String]).enumerate() { let menuItem = keyboardMenu.addItemWithTitle(theme, action: "changeKeyboardHandler:", keyEquivalent: "")! menuItem.tag = index } @@ -85,8 +81,8 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { case "Examples": menu.removeAllItems() examples = [String]() - if let arduinoPath = NSWorkspace.sharedWorkspace().URLForApplicationWithBundleIdentifier("cc.arduino.Arduino")?.path { - let examplePath = arduinoPath.stringByAppendingPathComponent("Contents/Resources/Java/examples") + if let arduinoURL = NSWorkspace.sharedWorkspace().URLForApplicationWithBundleIdentifier("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) } case "Import Standard Library": @@ -100,7 +96,7 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { while menu.numberOfItems > 2 { menu.removeItemAtIndex(2) } - for port in ASSerial.ports() as! [String] { + for port in ASSerial.ports() { menu.addItemWithTitle(port, action:"serialConnectMenu:", keyEquivalent:"") } default: @@ -119,28 +115,26 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { let oldName = template.lastPathComponent! let newName = saveTo.lastPathComponent! let fileManager = NSFileManager.defaultManager() - fileManager.copyItemAtURL(template, toURL: saveTo, error: nil) - let contents = fileManager.enumeratorAtURL(saveTo, - includingPropertiesForKeys: [NSURLNameKey, NSURLPathKey], - options: .SkipsHiddenFiles, errorHandler: nil) - while let item = contents?.nextObject() as? NSURL { - var renameItem = false - var itemName = item.lastPathComponent! - if itemName.stringByDeletingPathExtension == oldName { - renameItem = true - itemName = newName.stringByAppendingPathExtension(itemName.pathExtension)! - } - if renameItem { - fileManager.moveItemAtURL(item, - toURL: item.URLByDeletingLastPathComponent!.URLByAppendingPathComponent(itemName), - error: nil) + do { + try fileManager.copyItemAtURL(template, toURL: saveTo) + let contents = fileManager.enumeratorAtURL(saveTo, + includingPropertiesForKeys: [NSURLNameKey, NSURLPathKey], + options: .SkipsHiddenFiles, errorHandler: nil) + while let item = contents?.nextObject() as? NSURL { + let itemBase = item.URLByDeletingPathExtension?.lastPathComponent! + if itemBase == oldName { + let newItem = item.URLByDeletingLastPathComponent!.URLByAppendingPathComponent( + newName).URLByAppendingPathExtension(item.pathExtension!) + try fileManager.moveItemAtURL(item, toURL: newItem) + } } + } catch (_) { } let sketch = ASSketchBook.findSketch(saveTo.path!) switch sketch { case .Sketch(_, let path): - let doc = NSDocumentController.sharedDocumentController() as! NSDocumentController - doc.openDocumentWithContentsOfURL(NSURL(fileURLWithPath: path)!, display: true) { (doc, alreadyOpen, error) -> Void in + let doc = NSDocumentController.sharedDocumentController() + doc.openDocumentWithContentsOfURL(NSURL(fileURLWithPath: path), display: true) { (doc, alreadyOpen, error) -> Void in } default: break @@ -149,39 +143,40 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { } @IBAction func openSketch(item: NSMenuItem) { - if let url = NSURL(fileURLWithPath: sketches[item.tag]) { - let doc = NSDocumentController.sharedDocumentController() as! NSDocumentController - doc.openDocumentWithContentsOfURL(url, display: true) { (doc, alreadyOpen, error) -> Void in - } + let url = NSURL(fileURLWithPath: sketches[item.tag]) + let doc = NSDocumentController.sharedDocumentController() + doc.openDocumentWithContentsOfURL(url, display: true) { (doc, alreadyOpen, error) -> Void in } } @IBAction func openExample(item: NSMenuItem) { - if let url = NSURL(fileURLWithPath: examples[item.tag]) { - openTemplate(url.URLByDeletingLastPathComponent!) - } + let url = NSURL(fileURLWithPath: examples[item.tag]) + openTemplate(url.URLByDeletingLastPathComponent!) } - @IBAction func createSketch(AnyObject) { + @IBAction func createSketch(_: AnyObject) { ASApplication.newProjectLocation(nil, message: "Create Project") { (saveTo) -> Void in let fileManager = NSFileManager.defaultManager() - fileManager.createDirectoryAtURL(saveTo, withIntermediateDirectories:false, attributes:nil, error:nil) - let proj = saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent!+".avrsackproj") - let docController = NSDocumentController.sharedDocumentController() as! NSDocumentController - if let doc = docController.openUntitledDocumentAndDisplay(true, error:nil) as? ASProjDoc { - doc.fileURL = proj - doc.updateProjectURL() - doc.createFileAtURL(saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent!+".ino")) - doc.writeToURL(proj, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil, error: nil) + do { + try fileManager.createDirectoryAtURL(saveTo, withIntermediateDirectories:false, attributes:nil) + let proj = saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent!+".avrsackproj") + let docController = NSDocumentController.sharedDocumentController() + if let doc = try docController.openUntitledDocumentAndDisplay(true) as? ASProjDoc { + doc.fileURL = proj + doc.updateProjectURL() + doc.createFileAtURL(saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent!+".ino")) + try doc.writeToURL(proj, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil) + } + } catch _ { } } } class func newProjectLocation(documentWindow: NSWindow?, message: String, completion: (NSURL) -> ()) { let savePanel = NSSavePanel() - savePanel.allowedFileTypes = [kUTTypeFolder] + savePanel.allowedFileTypes = [kUTTypeFolder as String] savePanel.message = message if let window = documentWindow { savePanel.beginSheetModalForWindow(window, completionHandler: { (returnCode) -> Void in diff --git a/AVRsack/ASBuilder.swift b/AVRsack/ASBuilder.swift index 83f4d9c..09472a7 100644 --- a/AVRsack/ASBuilder.swift +++ b/AVRsack/ASBuilder.swift @@ -17,7 +17,7 @@ class ASBuilder { init() { termination = NSNotificationCenter.defaultCenter().addObserverForName(NSTaskDidTerminateNotification, object: nil, queue: nil, usingBlock: - { (notification: NSNotification!) in + { (notification: NSNotification) in if notification.object as? NSTask == self.task { if self.task!.terminationStatus == 0 { if let cont = self.continuation { @@ -44,7 +44,10 @@ class ASBuilder { } func cleanProject() { - NSFileManager.defaultManager().removeItemAtURL(dir.URLByAppendingPathComponent("build"), error: nil) + do { + try NSFileManager.defaultManager().removeItemAtURL(dir.URLByAppendingPathComponent("build")) + } catch _ { + } } func buildProject(board: String, files: ASFileTree) { @@ -62,7 +65,7 @@ class ASBuilder { } let boardProp = ASHardware.instance().boards[board]! let library = boardProp["library"]! - var corePath = library+"/cores/"+boardProp["build.core"]! + let corePath = library+"/cores/"+boardProp["build.core"]! var variantPath : String? if fileManager.fileExistsAtPath(corePath) { if let variantName = boardProp["build.variant"] { @@ -186,7 +189,7 @@ class ASBuilder { task2.standardOutput = logOut task2.standardError = logOut continuation = { - let cmdLine = task2.launchPath+" "+(loaderArgs as NSArray).componentsJoinedByString(" ")+"\n" + let cmdLine = task2.launchPath!+" "+(loaderArgs as NSArray).componentsJoinedByString(" ")+"\n" logOut.writeData(cmdLine.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!) task2.launch() self.continuation = { @@ -226,12 +229,12 @@ class ASBuilder { } } } - let cmdLine = task!.launchPath+" "+(args as NSArray).componentsJoinedByString(" ")+"\n" + let cmdLine = task!.launchPath!+" "+(args as NSArray).componentsJoinedByString(" ")+"\n" logOut.writeData(cmdLine.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!) task!.arguments = args; task!.launch() if interactive { - let intSpeed = speed?.toInt() ?? 19200 + let intSpeed = (speed != nil) ? Int(speed!) ?? 19200 : 19200 ASSerialWin.showWindowWithPort(port, task:task!, speed:intSpeed) task = nil } @@ -253,7 +256,7 @@ class ASBuilder { let showSource = NSUserDefaults.standardUserDefaults().boolForKey("ShowSourceInDisassembly") var args = showSource ? ["-S"] : [] args += ["-d", "build/"+board+"/"+dir.lastPathComponent!+".elf"] - let cmdLine = task!.launchPath+" "+(args as NSArray).componentsJoinedByString(" ")+"\n" + let cmdLine = task!.launchPath!+" "+(args as NSArray).componentsJoinedByString(" ")+"\n" logOut.writeData(cmdLine.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!) task!.arguments = args; task!.launch() diff --git a/AVRsack/ASFileTree.swift b/AVRsack/ASFileTree.swift index c22ea56..b30e378 100644 --- a/AVRsack/ASFileTree.swift +++ b/AVRsack/ASFileTree.swift @@ -54,12 +54,7 @@ private let kNodeTypeGroup = "Group" private let kNodeTypeFile = "File" private let kNameKey = "Name" -// -// At the moment, Swift crashes at link time with an assertion -// if anything other than a value type or an @objc class is put into a container -// exposed to ObjC APIs. As a workaround, we declare this hierarchy @objc -// -@objc class ASFileNode { +class ASFileNode { var name : String init(name: String) { @@ -192,7 +187,7 @@ class ASFileItem : ASFileNode { if let relativeURL = NSURL(string: prop[kPathKey] as! String, relativeToURL: rootURL) { url = relativeURL.URLByStandardizingPath! } else { - url = NSURL(fileURLWithPath:(prop[kPathKey] as! String))!.URLByStandardizingPath! + url = NSURL(fileURLWithPath:(prop[kPathKey] as! String)).URLByStandardizingPath! } if !url.checkResourceIsReachableAndReturnError(nil) { // @@ -200,10 +195,11 @@ class ASFileItem : ASFileNode { // yet reflected in the project file. // let urlDir = url.URLByDeletingLastPathComponent - let newName = rootURL.lastPathComponent!.stringByAppendingPathExtension(url.pathExtension!)! - let altURL = urlDir?.URLByAppendingPathComponent(newName) - if altURL != nil && altURL!.checkResourceIsReachableAndReturnError(nil) { - url = altURL! + let newName = rootURL.URLByAppendingPathExtension(url.pathExtension!).lastPathComponent! + if let altURL = urlDir?.URLByAppendingPathComponent(newName) { + if altURL.checkResourceIsReachableAndReturnError(nil) { + url = altURL + } } } super.init(name:url.lastPathComponent!) @@ -213,7 +209,7 @@ class ASFileItem : ASFileNode { } func relativePath(relativeTo: String) -> String { - let path = url.path!.stringByResolvingSymlinksInPath + let path = (url.path! as NSString).stringByResolvingSymlinksInPath let relComp = relativeTo.componentsSeparatedByString("/") as [String] let pathComp = path.componentsSeparatedByString("/") as [String] let relCount = relComp.count @@ -232,7 +228,7 @@ class ASFileItem : ASFileNode { } let resComp = Array(count: relCount-matchComp, repeatedValue: "..")+pathComp[matchComp.. AnyObject { return [kTypeKey: kNodeTypeFile, kKindKey: type.rawValue, kPathKey: relativePath(rootPath)] @@ -245,8 +241,12 @@ class ASFileItem : ASFileNode { } override func modDate() -> NSDate? { var date: AnyObject? - url.getResourceValue(&date, forKey: NSURLContentModificationDateKey, error: nil) - return date as? NSDate + do { + try url.getResourceValue(&date, forKey: NSURLContentModificationDateKey) + return date as? NSDate + } catch _ { + return nil + } } override func revision() -> String? { let task = NSTask() @@ -276,11 +276,11 @@ class ASFileTree : NSObject, NSOutlineViewDataSource { } } func setProjectURL(url: NSURL) { - root.name = url.lastPathComponent!.stringByDeletingPathExtension + root.name = url.URLByDeletingPathExtension!.lastPathComponent! dir = url.URLByDeletingLastPathComponent!.URLByStandardizingPath! } func projectPath() -> String { - return dir.path!.stringByResolvingSymlinksInPath + return (dir.path! as NSString).stringByResolvingSymlinksInPath } func apply(closure: (ASFileNode) -> ()) { root.apply(closure) diff --git a/AVRsack/ASHardware.swift b/AVRsack/ASHardware.swift index 6ce8058..1789eae 100644 --- a/AVRsack/ASHardware.swift +++ b/AVRsack/ASHardware.swift @@ -13,7 +13,7 @@ typealias ASProperties = [String: ASPropertyEntry] extension NSMenu { func addSortedChoices(choices:[ASPropertyEntry], target: AnyObject, selector: Selector) { - for choice in choices.sorted({ $0["name"] < $1["name"] }) { + for choice in choices.sort({ $0["name"] < $1["name"] }) { let item = self.addItemWithTitle(choice["name"]!, action: selector, keyEquivalent: "") item?.target = target } @@ -24,8 +24,8 @@ private func subdirectories(path: String) -> [String] { let fileManager = NSFileManager.defaultManager() var subDirs = [String]() var isDir : ObjCBool = false - if fileManager.fileExistsAtPath(path, isDirectory: &isDir) && isDir { - for item in fileManager.contentsOfDirectoryAtPath(path, error: nil) as! [String] { + if let items = try? fileManager.contentsOfDirectoryAtPath(path) { + for item in items { let subPath = path+"/"+item if fileManager.fileExistsAtPath(subPath, isDirectory: &isDir) && isDir { subDirs.append(subPath) @@ -46,7 +46,6 @@ class ASHardware { // Gather hardware directories // let userDefaults = NSUserDefaults.standardUserDefaults() - let fileManager = NSFileManager.defaultManager() if let arduinoPath = userDefaults.stringForKey("Arduino") { let arduinoHardwarePath = arduinoPath + "/Contents/Resources/Java/hardware" directories += subdirectories(arduinoHardwarePath) @@ -55,20 +54,21 @@ class ASHardware { let hardwarePath = sketchDir + "/hardware" directories += subdirectories(hardwarePath) } - let property = NSRegularExpression(pattern: "\\s*(\\w+)\\.(\\S+?)\\s*=\\s*(\\S.*\\S)\\s*", options: nil, error: nil)! + let property = try! NSRegularExpression(pattern: "\\s*(\\w+)\\.(\\S+?)\\s*=\\s*(\\S.*\\S)\\s*", options: []) // // Gather board declarations // for dir in directories { let boardsPath = dir+"/boards.txt" - let provenience = dir.lastPathComponent - if let boardsFile = NSString(contentsOfFile: boardsPath, usedEncoding: nil, error: nil) { + let provenience = (dir as NSString).lastPathComponent + if let boardsFile = try? NSString(contentsOfFile: boardsPath, usedEncoding: nil) { var seen = [String: Bool]() - for line in boardsFile.componentsSeparatedByString("\n") as! [NSString] { - if let match = property.firstMatchInString(line as String, options: .Anchored, range: NSMakeRange(0, line.length)) { - let board = line.substringWithRange(match.rangeAtIndex(1)) as String - let property = line.substringWithRange(match.rangeAtIndex(2)) as String - let value = line.substringWithRange(match.rangeAtIndex(3)) as String + for line in boardsFile.componentsSeparatedByString("\n") { + if let match = property.firstMatchInString(line, options: .Anchored, range: NSMakeRange(0, line.utf16.count)) { + let nsline = line as NSString + let board = nsline.substringWithRange(match.rangeAtIndex(1)) as String + let property = nsline.substringWithRange(match.rangeAtIndex(2)) as String + let value = nsline.substringWithRange(match.rangeAtIndex(3)) as String if seen.updateValue(true, forKey: board) == nil { boards[board] = ASPropertyEntry() boards[board]!["provenience"] = provenience @@ -85,14 +85,15 @@ class ASHardware { // for dir in directories { let programmersPath = dir+"/programmers.txt" - let provenience = dir.lastPathComponent - if let programmersFile = NSString(contentsOfFile: programmersPath, usedEncoding: nil, error: nil) { + let provenience = (dir as NSString).lastPathComponent + if let programmersFile = try? NSString(contentsOfFile: programmersPath, usedEncoding: nil) { var seen = [String: Bool]() - for line in programmersFile.componentsSeparatedByString("\n") as! [NSString] { - if let match = property.firstMatchInString(line as String, options: .Anchored, range: NSMakeRange(0, line.length)) { - let programmer = line.substringWithRange(match.rangeAtIndex(1)) - let property = line.substringWithRange(match.rangeAtIndex(2)) - let value = line.substringWithRange(match.rangeAtIndex(3)) + for line in programmersFile.componentsSeparatedByString("\n") { + if let match = property.firstMatchInString(line, options: .Anchored, range: NSMakeRange(0, line.utf16.count)) { + let nsline = line as NSString + let programmer = nsline.substringWithRange(match.rangeAtIndex(1)) + let property = nsline.substringWithRange(match.rangeAtIndex(2)) + let value = nsline.substringWithRange(match.rangeAtIndex(3)) if seen.updateValue(true, forKey: programmer) == nil { programmers[programmer] = ASPropertyEntry() programmers[programmer]!["provenience"] = provenience @@ -117,7 +118,7 @@ class ASHardware { seen[prop["provenience"]!] = true } var sortedKeys = [String](seen.keys) - sortedKeys.sort { $0 < $1 } + sortedKeys.sortInPlace { $0 < $1 } for provenience in sortedKeys { var subset = [ASPropertyEntry]() for prop in choices.values { @@ -155,7 +156,6 @@ class ASLibraries : NSObject { // Gather hardware directories // let userDefaults = NSUserDefaults.standardUserDefaults() - let fileManager = NSFileManager.defaultManager() for sketchDir in userDefaults.objectForKey("Sketchbooks") as! [String] { let librariesPath = sketchDir + "/libraries" let dirs = subdirectories(librariesPath) @@ -176,15 +176,15 @@ class ASLibraries : NSObject { } } func addStandardLibrariesToMenu(menu: NSMenu) { - for (index,lib) in enumerate(standardLib) { - let menuItem = menu.addItemWithTitle(lib.lastPathComponent, action: "importStandardLibrary:", keyEquivalent: "") + for (index,lib) in standardLib.enumerate() { + let menuItem = menu.addItemWithTitle((lib as NSString).lastPathComponent, action: "importStandardLibrary:", keyEquivalent: "") menuItem?.target = self menuItem?.tag = index } } func addContribLibrariesToMenu(menu: NSMenu) { - for (index,lib) in enumerate(contribLib) { - let menuItem = menu.addItemWithTitle(lib.lastPathComponent, action: "importContribLibrary:", keyEquivalent: "") + for (index,lib) in contribLib.enumerate() { + let menuItem = menu.addItemWithTitle((lib as NSString).lastPathComponent, action: "importContribLibrary:", keyEquivalent: "") menuItem?.target = self menuItem?.tag = index } diff --git a/AVRsack/ASProjDoc.swift b/AVRsack/ASProjDoc.swift index 50beab5..9389409 100644 --- a/AVRsack/ASProjDoc.swift +++ b/AVRsack/ASProjDoc.swift @@ -14,7 +14,7 @@ private var keyboardHandler : ACEKeyboardHandler = .Ace func pushToFront(inout list: [String], front: String) { let kMaxRecents = 8 - if let existing = find(list, front) { + if let existing = list.indexOf(front) { if existing == 0 { return } else { @@ -48,7 +48,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa dynamic var port : String = "" var recentBoards = [String]() var recentProgrammers = [String]() - var logModified = NSDate.distantPast() as! NSDate + var logModified = NSDate.distantPast() var logSize = 0 var updateLogTimer : NSTimer? var printingDone : () -> () = {} @@ -93,7 +93,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa recentBoards = userDefaults.objectForKey(kRecentBoardsKey) as! [String] recentProgrammers = userDefaults.objectForKey(kRecentProgrammersKey) as! [String] - var nc = NSNotificationCenter.defaultCenter() + let nc = NSNotificationCenter.defaultCenter() themeObserver = nc.addObserverForName(kBindingsKey, object: nil, queue: nil, usingBlock: { (NSNotification) in self.editor?.setKeyboardHandler(keyboardHandler) }) @@ -155,11 +155,14 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa func saveCurEditor() { if let file = (mainEditor as? ASFileItem) { - editor.string().writeToURL(file.url, atomically: true, encoding: NSUTF8StringEncoding, error: nil) + do { + try editor.string().writeToURL(file.url, atomically: true, encoding: NSUTF8StringEncoding) + } catch _ { + } } } - override func dataOfType(typeName: String, error outError: NSErrorPointer) -> NSData? { + override func dataOfType(typeName: String) throws -> NSData { let data = [kVersionKey: kCurVersion, kThemeKey: ACEThemeNames.nameForTheme(currentTheme), kFontSizeKey: fontSize, @@ -170,7 +173,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa kRecentBoardsKey: recentBoards, kRecentProgrammersKey: recentProgrammers ] - return NSPropertyListSerialization.dataWithPropertyList(data, format:.XMLFormat_v1_0, options:0, error:nil) + return try NSPropertyListSerialization.dataWithPropertyList(data, format:.XMLFormat_v1_0, options:0) } func updateProjectURL() { @@ -178,44 +181,40 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa builder.setProjectURL(fileURL!) } - func importProject(url: NSURL, error outError: NSErrorPointer) -> Bool { + func importProject(url: NSURL) throws { let existingProject = url.URLByAppendingPathComponent(url.lastPathComponent!+".avrsackproj") if existingProject.checkResourceIsReachableAndReturnError(nil) { fileURL = existingProject - return readFromURL(existingProject, ofType:"Project", error:outError) + try readFromURL(existingProject, ofType:"Project") + return } let filesInProject = - NSFileManager.defaultManager().contentsOfDirectoryAtURL(url, includingPropertiesForKeys: nil, - options: .SkipsHiddenFiles, error: nil) as! [NSURL] + (try NSFileManager.defaultManager().contentsOfDirectoryAtURL(url, includingPropertiesForKeys: nil, + options: .SkipsHiddenFiles)) updateProjectURL() for file in filesInProject { files.addFileURL(file) } - return true } - override func readFromURL(url: NSURL, ofType typeName: String, error outError: NSErrorPointer) -> Bool { - var success : Bool = false + override func readFromURL(url: NSURL, ofType typeName: String) throws { if typeName == "Arduino Source File" { let projectURL = url.URLByDeletingPathExtension!.URLByAppendingPathExtension("avrsackproj") - success = importProject(url.URLByDeletingLastPathComponent!, error: outError) - if success { - fileURL = projectURL - success = writeToURL(projectURL, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil, error: outError) - } - } else { + try importProject(url.URLByDeletingLastPathComponent!) + fileURL = projectURL + try writeToURL(projectURL, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil) + } else { fileURL = url - success = super.readFromURL(url, ofType: typeName, error: outError) + try super.readFromURL(url, ofType: typeName) } - return success } - override func readFromData(data: NSData, ofType typeName: String, error outError: NSErrorPointer) -> Bool { + override func readFromData(data: NSData, ofType typeName: String) throws { if typeName != ("Project" as String) { - return false + throw NSError(domain: "AVRSack", code: 0, userInfo: nil) } updateProjectURL() let projectData = - NSPropertyListSerialization.propertyListWithData(data, options:0, format:nil, error:nil) as! NSDictionary + (try NSPropertyListSerialization.propertyListWithData(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 { @@ -233,8 +232,6 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa recentBoards = (projectData[kRecentBoardsKey] as? [String]) ?? recentBoards recentProgrammers = (projectData[kRecentProgrammersKey] as? [String]) ?? recentProgrammers updateChangeCount(.ChangeCleared) - - return true } override func duplicateDocument(sender: AnyObject?) { @@ -242,7 +239,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa app.openTemplate(fileURL!.URLByDeletingLastPathComponent!) } - func updateLog(AnyObject?) { + func updateLog(_: AnyObject?) { if let logNode = mainEditor as? ASLogNode { let url = fileURL?.URLByDeletingLastPathComponent?.URLByAppendingPathComponent(logNode.path) if url == nil { @@ -250,16 +247,16 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa } var modified : AnyObject? var size : AnyObject? - if (!url!.getResourceValue(&modified, forKey:NSURLAttributeModificationDateKey, error:nil)) { + do { + try url!.getResourceValue(&modified, forKey:NSURLAttributeModificationDateKey) + try url!.getResourceValue(&size, forKey:NSURLFileSizeKey) + } catch (_) { return } - if (!url!.getResourceValue(&size, forKey:NSURLFileSizeKey, error:nil)) { - return - } - + if (modified as! NSDate).compare(logModified) == .OrderedDescending || (size as! Int) != logSize { var enc : UInt = 0 - let newText = NSString(contentsOfURL:url!, usedEncoding:&enc, error:nil) + let newText = try? NSString(contentsOfURL:url!, usedEncoding:&enc) editor.setString((newText as? String) ?? "") editor.gotoLine(1000000000, column: 0, animated: true) logModified = modified as! NSDate @@ -274,15 +271,16 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa } if let file = (selection as? ASFileItem) { var enc : UInt = 0 - editor.setString(NSString(contentsOfURL:file.url, usedEncoding:&enc, error:nil) as? String ?? "") + let contents = try? NSString(contentsOfURL:file.url, usedEncoding:&enc) + editor.setString(contents as? String ?? "") editor.setMode(file.type.aceMode) editor.alphaValue = 1.0 mainEditor = selection - } else if let log = (selection as? ASLogNode) { + } else if selection is ASLogNode { editor.setString("") editor.setMode(.Text) editor.alphaValue = 0.8 - logModified = NSDate.distantPast() as! NSDate + logModified = NSDate.distantPast() logSize = -1 mainEditor = selection updateLog(nil) @@ -306,18 +304,20 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa // MARK: Printing - override func printDocumentWithSettings(printSettings: [NSObject : AnyObject], showPrintPanel: Bool, delegate: AnyObject?, didPrintSelector: Selector, contextInfo: UnsafeMutablePointer) { + override func printDocumentWithSettings(printSettings: [String : AnyObject], showPrintPanel: Bool, delegate: AnyObject?, didPrintSelector: Selector, contextInfo: UnsafeMutablePointer) { printingDone = { () -> () in InvokeCallback(delegate, didPrintSelector, contextInfo); } if let logNode = mainEditor as? ASLogNode { - let url = fileURL!.URLByDeletingLastPathComponent?.URLByAppendingPathComponent(logNode.path) - var modified : AnyObject? - if url?.getResourceValue(&modified, forKey:NSURLAttributeModificationDateKey, error:nil) != nil { - printModDate = modified as? NSDate - } else { - printModDate = nil + 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 (_) { + } } } else { printModDate = mainEditor?.modDate() @@ -329,7 +329,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa } func printInformation() -> NSPrintInfo! { - var info = printInfo.copy() as! NSPrintInfo + let info = printInfo.copy() as! NSPrintInfo // // Minimize margins @@ -358,9 +358,13 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa } func startPrintOperation(printOp: NSPrintOperation) { - printOp.jobTitle = mainEditor?.name ?? - fileURL?.lastPathComponent?.stringByDeletingPathExtension ?? - "Untitled" + if let editorName = mainEditor?.name { + printOp.jobTitle = editorName + } else if let fileName = fileURL?.lastPathComponent { + printOp.jobTitle = (fileName as NSString).stringByDeletingLastPathComponent + } else { + printOp.jobTitle = "Untitled" + } printOp.showsPrintPanel = printShowPanel } @@ -414,10 +418,11 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa if let fileNameStr = mainEditor?.name { fileNameStr.drawAtPoint(titleAt, withAttributes:titleAttr) } - if let projectNameStr = fileURL?.lastPathComponent?.stringByDeletingPathExtension { - let projectNameSize = projectNameStr.sizeWithAttributes(titleAttr) + if let projectNameStr = fileURL?.lastPathComponent { + let projectNameTrimmed = (projectNameStr as NSString).stringByDeletingPathExtension + let projectNameSize = projectNameTrimmed.sizeWithAttributes(titleAttr) titleAt.x = wideBox.origin.x+wideBox.size.width-projectNameSize.width-kXOffset - projectNameStr.drawAtPoint(titleAt, withAttributes:titleAttr) + projectNameTrimmed.drawAtPoint(titleAt, withAttributes:titleAttr) } } @@ -494,7 +499,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa // MARK: File manipulation - @IBAction func delete(AnyObject) { + @IBAction func delete(_: AnyObject) { let selection = selectedFiles() var name : String var ref : String @@ -511,8 +516,8 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa alert.addButtonWithTitle("Move to Trash") alert.addButtonWithTitle(selection.count == 1 ? "Remove Reference" : "Remove References") alert.addButtonWithTitle("Cancel") - (alert.buttons[0] as! NSButton).keyEquivalent = "" - (alert.buttons[1] as! NSButton).keyEquivalent = "\r" + alert.buttons[0].keyEquivalent = "" + alert.buttons[1].keyEquivalent = "\r" alert.beginSheetModalForWindow(outline.window!) { (response) in if response != NSAlertThirdButtonReturn { if response == NSAlertFirstButtonReturn { @@ -521,7 +526,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa self.files.apply { (node) in if let group = node as? ASFileGroup { for file in selection { - for (groupIdx, groupItem) in enumerate(group.children) { + for (groupIdx, groupItem) in group.children.enumerate() { if file as ASFileNode === groupItem { group.children.removeAtIndex(groupIdx) break @@ -537,7 +542,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa } } - @IBAction func add(AnyObject) { + @IBAction func add(_: AnyObject) { let panel = NSOpenPanel() panel.canChooseFiles = true panel.canChooseDirectories = false @@ -546,7 +551,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa panel.delegate = self panel.beginSheetModalForWindow(outline.window!, completionHandler: { (returnCode: Int) -> Void in if returnCode == NSFileHandlingPanelOKButton { - for url in panel.URLs as! [NSURL] { + for url in panel.URLs { self.files.addFileURL(url) } self.outline.deselectAll(self) @@ -560,13 +565,16 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa func panel(panel:AnyObject, shouldEnableURL url:NSURL) -> Bool { var shouldEnable = true var resourceID : AnyObject? - url.getResourceValue(&resourceID, forKey:NSURLFileResourceIdentifierKey, error:nil) + guard ((try? url.getResourceValue(&resourceID, forKey:NSURLFileResourceIdentifierKey)) != nil) else { + return true; + } files.apply {(node) in if let file = node as? ASFileItem { var thisID : AnyObject? - file.url.getResourceValue(&thisID, forKey:NSURLFileResourceIdentifierKey, error:nil) - if thisID != nil && resourceID!.isEqual(thisID!) { - shouldEnable = false + if (try? file.url.getResourceValue(&thisID, forKey:NSURLFileResourceIdentifierKey)) != nil { + if thisID != nil && resourceID!.isEqual(thisID!) { + shouldEnable = false + } } } } @@ -614,15 +622,18 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa prefix + " Created: " + dateFmt.stringFromDate(NSDate()) + "\n" + lastPfx + "\n\n" } - header.writeToURL(url, atomically: true, encoding: NSUTF8StringEncoding, error: nil) + do { + try header.writeToURL(url, atomically: true, encoding: NSUTF8StringEncoding) + } catch _ { + } files.addFileURL(url) outline.reloadData() } - @IBAction func createFile(AnyObject) { + @IBAction func createFile(_: AnyObject) { let savePanel = NSSavePanel() savePanel.allowedFileTypes = - [kUTTypeCSource, kUTTypeCHeader, kUTTypeCPlusPlusSource, kUTTypeCSource, + [kUTTypeCSource as String, kUTTypeCHeader as String, kUTTypeCPlusPlusSource as String, kUTTypeAssemblyLanguageSource as String, "public.assembly-source", "net.daringfireball.markdown"] savePanel.beginSheetModalForWindow(outline.window!, completionHandler: { (returnCode) -> Void in if returnCode == NSFileHandlingPanelOKButton { @@ -634,14 +645,16 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa func importLibrary(lib: String) { var includes = "" let fileManager = NSFileManager.defaultManager() - for file in fileManager.contentsOfDirectoryAtPath(lib, error: nil) as! [String] { - if file.hasSuffix(".h") { - includes += "#include <\(file)>\n" + if let files = try? fileManager.contentsOfDirectoryAtPath(lib) { + for file in files { + if file.hasSuffix(".h") { + includes += "#include <\(file)>\n" + } } } - var text = editor.string() as NSString + let text = editor.string() as NSString var insert = NSMakeRange(text.length, 0) - let postHeaderComments = NSRegularExpression(pattern: "((?:\\s+|/\\*.*?\\*/|//.*?\\n)*)(.*?\\n)", options: .DotMatchesLineSeparators, error: nil)! + let postHeaderComments = try! NSRegularExpression(pattern: "((?:\\s+|/\\*.*?\\*/|//.*?\\n)*)(.*?\\n)", options: .DotMatchesLineSeparators) if let match = postHeaderComments.firstMatchInString(text as String, options:.Anchored, range:NSMakeRange(0, text.length)) { let range = match.rangeAtIndex(2) insert.location = range.location @@ -691,12 +704,12 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa return super.validateUserInterfaceItem(anItem) } - @IBAction func makeTextLarger(AnyObject) { + @IBAction func makeTextLarger(_: AnyObject) { fontSize += 1 editor.setFontSize(fontSize) updateChangeCount(.ChangeDone) } - @IBAction func makeTextSmaller(AnyObject) { + @IBAction func makeTextSmaller(_: AnyObject) { if fontSize > 6 { fontSize -= 1 editor.setFontSize(fontSize) @@ -715,41 +728,44 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa return } var enc : UInt = 0 - auxEdit.setString(NSString(contentsOfURL:url!, usedEncoding:&enc, error:nil) as? String ?? "") + let contents = try? NSString(contentsOfURL:url!, usedEncoding:&enc) + auxEdit.setString(contents as? String ?? "") editor.setMode(.Text) editor.alphaValue = 1.0 } let buildLog = auxEdit.string().componentsSeparatedByString("\n") - let issueRe = NSRegularExpression(pattern: "(\\S+?):(\\d+):.*", options: nil, error: nil)! + let issueRe = try! NSRegularExpression(pattern: "(\\S+?):(\\d+):.*", options: []) currentIssueLine += direction while currentIssueLine > -1 && currentIssueLine < buildLog.count { let line = buildLog[currentIssueLine] - let range = NSMakeRange(0, count(line.utf16)) + 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) let nsline = line as NSString - let lineNo = nsline.substringWithRange(lineTxt).toInt()! + let lineNo = Int(nsline.substringWithRange(lineTxt))! let fileName = nsline.substringWithRange(file) as NSString let fileURL : NSURL if fileName.hasPrefix("../../") { fileURL = files.dir.URLByAppendingPathComponent(fileName.substringFromIndex(6)) } else { - fileURL = NSURL(fileURLWithPath:fileName as String)!.URLByStandardizingPath! + fileURL = NSURL(fileURLWithPath:fileName as String).URLByStandardizingPath! } jumpingToIssue = true var resourceID : AnyObject? - fileURL.getResourceValue(&resourceID, forKey:NSURLFileResourceIdentifierKey, error:nil) - files.apply {(node) in - if let file = node as? ASFileItem { - var thisID : AnyObject? - file.url.getResourceValue(&thisID, forKey:NSURLFileResourceIdentifierKey, error:nil) - if thisID != nil && resourceID!.isEqual(thisID!) { - self.selectNodeInOutline(node) - self.editor.gotoLine(lineNo, column:0, animated:true) + if (try? fileURL.getResourceValue(&resourceID, forKey:NSURLFileResourceIdentifierKey)) != nil && resourceID != nil { + files.apply {(node) in + if let file = node as? ASFileItem { + var thisID : AnyObject? + if (try? file.url.getResourceValue(&thisID, forKey:NSURLFileResourceIdentifierKey)) != nil { + if thisID != nil && resourceID!.isEqual(thisID!) { + self.selectNodeInOutline(node) + self.editor.gotoLine(lineNo, column:0, animated:true) + } + } } } } @@ -764,12 +780,12 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa // MARK: Build / Upload - @IBAction func buildProject(AnyObject) { + @IBAction func buildProject(_: AnyObject) { selectNodeInOutline(files.buildLog) builder.buildProject(board, files: files) } - @IBAction func cleanProject(AnyObject) { + @IBAction func cleanProject(_: AnyObject) { builder.cleanProject() selectNodeInOutline(files.buildLog) } @@ -808,11 +824,11 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa if prop["name"] == newBoard { board = ident - pushToFront(&recentBoards, board) + pushToFront(&recentBoards, front: board) let userDefaults = NSUserDefaults.standardUserDefaults() var globalBoards = userDefaults.objectForKey(kRecentBoardsKey) as! [String] - pushToFront(&globalBoards, board) + pushToFront(&globalBoards, front: board) userDefaults.setObject(globalBoards, forKey: kRecentBoardsKey) updateChangeCount(.ChangeDone) @@ -838,11 +854,11 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa if prop["name"] == newProg { programmer = ident - pushToFront(&recentProgrammers, programmer) + pushToFront(&recentProgrammers, front: programmer) let userDefaults = NSUserDefaults.standardUserDefaults() var globalProgs = userDefaults.objectForKey(kRecentProgrammersKey) as! [String] - pushToFront(&globalProgs, programmer) + pushToFront(&globalProgs, front: programmer) userDefaults.setObject(globalProgs, forKey: kRecentProgrammersKey) updateChangeCount(.ChangeDone) @@ -879,7 +895,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate, NSOpenSavePa var hasValidPort : Bool { get { - return contains(ASSerial.ports() as! [String], port) + return ASSerial.ports().contains(port) } } class func keyPathsForValuesAffectingHasValidPort() -> NSSet { diff --git a/AVRsack/ASSerial.h b/AVRsack/ASSerial.h index 1a6182f..acff227 100644 --- a/AVRsack/ASSerial.h +++ b/AVRsack/ASSerial.h @@ -19,7 +19,7 @@ extern NSString * kASSerialPortsChanged; @interface ASSerial : NSObject + (NSString *) fileNameForPort:(NSString *)port; -+ (NSArray *) ports; ++ (NSArray *) ports; + (NSFileHandle *)openPort:(NSString *) port withSpeed:(int)speed; + (void)restorePort:(int)fileDescriptor; + (void)closePort:(int)fileDescriptor; diff --git a/AVRsack/ASSerial.mm b/AVRsack/ASSerial.mm index 4f3a9ea..c391ec3 100644 --- a/AVRsack/ASSerial.mm +++ b/AVRsack/ASSerial.mm @@ -42,7 +42,7 @@ NSString * kASSerialPortsChanged = @"PortsChanged"; dispatch_resume(watchSlashDev); } -+ (NSArray *)ports { ++ (NSArray *)ports { NSMutableArray * cuPorts = [NSMutableArray array]; for (NSString * port in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/dev" error: nil]) { if ([[port substringToIndex:2] isEqualToString:@"cu"]) diff --git a/AVRsack/ASSerialWin.swift b/AVRsack/ASSerialWin.swift index 9f79b11..8f1967c 100644 --- a/AVRsack/ASSerialWin.swift +++ b/AVRsack/ASSerialWin.swift @@ -103,7 +103,7 @@ class ASSerialWin: NSWindowController { } } - var nc = NSNotificationCenter.defaultCenter() + let nc = NSNotificationCenter.defaultCenter() serialObserver = nc.addObserverForName(kASSerialPortsChanged, object: nil, queue: nil, usingBlock: { (NSNotification) in self.willChangeValueForKey("hasValidPort") self.didChangeValueForKey("hasValidPort") @@ -118,7 +118,7 @@ class ASSerialWin: NSWindowController { }) termination = NSNotificationCenter.defaultCenter().addObserverForName(NSTaskDidTerminateNotification, object: nil, queue: nil, usingBlock: - { (notification: NSNotification!) in + { (notification: NSNotification) in if notification.object as? NSTask == self.task { self.task = nil self.portHandle = nil @@ -174,7 +174,7 @@ class ASSerialWin: NSWindowController { } } - @IBAction func sendInput(AnyObject) { + @IBAction func sendInput(_: AnyObject) { let line = inputLine.stringValue + (sendCR ? "\r" : "") + (sendLF ? "\n" : "") let data = line.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)! portHandle?.writeData(data) @@ -191,7 +191,7 @@ class ASSerialWin: NSWindowController { installReader((task.standardOutput as? NSPipe)?.fileHandleForReading) } - @IBAction func connect(AnyObject) { + @IBAction func connect(_: AnyObject) { shouldReconnect = false if task != nil { task!.interrupt() @@ -227,7 +227,7 @@ class ASSerialWin: NSWindowController { } var hasValidPort : Bool { get { - return contains(ASSerial.ports() as! [String], port) + return ASSerial.ports().contains(port) } } @@ -262,13 +262,13 @@ class ASSerialWin: NSWindowController { return true } - @IBAction func makeTextLarger(AnyObject) { + @IBAction func makeTextLarger(_: AnyObject) { fontSize += 1 logView.setFontSize(fontSize) portDefaults["FontSize"] = fontSize updatePortDefaults() } - @IBAction func makeTextSmaller(AnyObject) { + @IBAction func makeTextSmaller(_: AnyObject) { if fontSize > 6 { fontSize -= 1 logView.setFontSize(fontSize) @@ -285,14 +285,17 @@ class ASSerialWin: NSWindowController { userDefaults.setObject(serialDefaults, forKey:"SerialDefaults") } - @IBAction func saveDocument(AnyObject) { + @IBAction func saveDocument(_: AnyObject) { let savePanel = NSSavePanel() savePanel.allowedFileTypes = ["log"] savePanel.allowsOtherFileTypes = true savePanel.extensionHidden = false savePanel.beginSheetModalForWindow(window!, completionHandler: { (returnCode) -> Void in if returnCode == NSFileHandlingPanelOKButton { - self.serialData.writeToURL(savePanel.URL!, atomically:false, encoding:NSUTF8StringEncoding, error:nil) + do { + try self.serialData.writeToURL(savePanel.URL!, atomically:false, encoding:NSUTF8StringEncoding) + } catch _ { + } } }) } diff --git a/AVRsack/ASSketchBook.swift b/AVRsack/ASSketchBook.swift index 7463d4a..0a5cb7d 100644 --- a/AVRsack/ASSketchBook.swift +++ b/AVRsack/ASSketchBook.swift @@ -18,13 +18,14 @@ class ASSketchBook { class func findSketch(path: String) -> SketchBookItem { let fileManager = NSFileManager.defaultManager() var inoSketch = SketchBookItem.Nothing - let contents = fileManager.contentsOfDirectoryAtPath(path, error: nil) as! [String] + let contents = (try! fileManager.contentsOfDirectoryAtPath(path)) + let nspath = path as NSString for item in contents { - switch item.pathExtension { + switch (item as NSString).pathExtension { case "avrsackproj": - return .Sketch(path.lastPathComponent, path.stringByAppendingPathComponent(item)) + return .Sketch(nspath.lastPathComponent, nspath.stringByAppendingPathComponent(item)) case "ino": - inoSketch = .Sketch(path.lastPathComponent, path.stringByAppendingPathComponent(item)) + inoSketch = .Sketch(nspath.lastPathComponent, nspath.stringByAppendingPathComponent(item)) default: break } @@ -34,7 +35,8 @@ class ASSketchBook { private class func enumerateSketches(path: String) -> SketchBookItem { let fileManager = NSFileManager.defaultManager() - let contents = fileManager.contentsOfDirectoryAtPath(path, error: nil) as! [String] + let contents = (try! fileManager.contentsOfDirectoryAtPath(path)) + let nspath = path as NSString let sketch = findSketch(path) switch sketch { case .Sketch: @@ -44,7 +46,7 @@ class ASSketchBook { } var sketches = [SketchBookItem]() for item in contents { - let subpath = path.stringByAppendingPathComponent(item) + let subpath = nspath.stringByAppendingPathComponent(item) var isDir : ObjCBool = false if fileManager.fileExistsAtPath(subpath, isDirectory: &isDir) && isDir { let subEnum = enumerateSketches(subpath) @@ -56,7 +58,7 @@ class ASSketchBook { } } } - sketches.sort({ (a: SketchBookItem, b: SketchBookItem) -> Bool in + sketches.sortInPlace({ (a: SketchBookItem, b: SketchBookItem) -> Bool in var itemA : String = "" switch a { case .Sketch(let item, _): @@ -75,7 +77,7 @@ class ASSketchBook { return itemA < "" } }) - return sketches.count > 0 ? .SketchDir(path.lastPathComponent, sketches) : .Nothing + return sketches.count > 0 ? .SketchDir(nspath.lastPathComponent, sketches) : .Nothing } class func appendSketchesToMenu(menu: NSMenu, target: AnyObject, action: Selector, sketchList: [SketchBookItem], inout sketches: [String]) { @@ -100,7 +102,7 @@ class ASSketchBook { class func addSketches(menu: NSMenu, target: AnyObject, action: Selector, path: String, inout sketches: [String]) { switch enumerateSketches(path) { - case .SketchDir(let item, let sketchList): + case .SketchDir(_, let sketchList): appendSketchesToMenu(menu, target: target, action: action, sketchList: sketchList, sketches: &sketches) default: break