Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift 2 in Production

Florian
November 17, 2015

Swift 2 in Production

Talk held at our local user group, http://mobilemaultaschen.de

Florian

November 17, 2015
Tweet

More Decks by Florian

Other Decks in Programming

Transcript

  1. Swift 2 In Production

  2. New Stuff ☞ guard ♥♥♥ ☞ #available() ♥ ☞ Protocol

    Extensions ♥♥ ☞ SDK ♥ ☞ Error Handling " ☞ defer "
  3. guard ☞ no more pyramid of doom optionals if let

    a = a { if let b = b { if let c = c { fn(a, b, c) } } }
  4. guard ☞ better, but still pretty unreadable ☞ still too

    much indentation if let a = a, let b = b, let c = c { fn(a, b, c) }
  5. guard guard let a = a else { Log.Error("a is

    required at this point.") return } guard let b = b else { Log.Error("b is required at this point.") return } guard let c = c else { Log.Error("c is required at this point.") return }
  6. #available() let iOS9 = NSOperatingSystemVersion( majorVersion: 9, minorVersion: 0, patchVersion:

    0) if NSProcessInfo().isOperatingSystemAtLeastVersion(iOS9) { // Stack views ! }
  7. #available() if #available(iOS 9, *) { // Stack views !

    } else { // Stack views " } @available(iOS 9, *) class MyStackView: UIStackView {}
  8. Protocol extensions ☞ replace base class ☞ default behaviour ☞

    decoration/composition ☞ adopt in enum/struct/class
  9. Protocol extensions @objc public protocol Bookmarkable { var remoteIdentifier: Int

    { get } } extension Bookmarkable { public var remoteIdentifier: Int { return -1 } }
  10. Protocol Extensions extension NSManagedObject: Bookmarkable { public var remoteIdentifier: Int

    { guard respondsToSelector("remoteID") else { Log.Warn("\(self) doesn't respond to 'remoteID'") return -1 } guard let remoteID = valueForKey("remoteID") as? NSNumber else { Log.Error("'remoteID' didn't return a number.") return -1 } return Int(remoteID.integerValue) } }
  11. Error handling ☞ good ol' times var readError: NSError? let

    contents = NSString(contentsOfFile: filePath, encoding: NSUTF8StringEncoding, error: &readError) if readError != nil { // Oh no, something went wrong }
  12. Error handling ☞ the new way: let contents: NSString? do

    { contents = try NSString(contentsOfFile: filePath, encoding: NSUTF8StringEncoding) } catch let error as NSError { print(error) }
  13. Error handling ☞ define your error types enum FileError: ErrorType

    { case NoSuchFile case IsDirectory } func readFile(atPath path: String) throws { var isDir: ObjCBool = false guard NSFileManager().fileExistsAtPath(path, isDirectory: &isDir) else { throw FileError.NoSuchFile } guard isDir.boolValue == false else { throw FileError.IsDirectory } }
  14. Error handling do { try readFile(atPath: "~/.vim") } catch FileError.NoSuchFile

    { print("No such file") } catch FileError.IsDirectory { print("Item at path is a directory") }
  15. defer func writeToStream(something: StreamWriteable) { let stream = openStream() defer

    { closeStream(stream) } something.write(toStream: stream) // don't worry about closing it // even when errors occur }
  16. Mixed Projects ☞ Setup ☞ bridging Header (only app targets)

    ☞ module map (framework targets) ☞ module name (requires DEFINES_MODULE=YES)
  17. Mixed Projects ☞ Ignore Xcode "Can't build module XYZ" ☞

    Ignore missing auto completion when importing "ModuleName- Swift.h"
  18. Debugger ☞ Can't jump into ObjC file from Swift code

    ☞ Can't jump into Swift file from ObjC code
  19. Dependencies ☞ Dependency management, iOS >= 8.0 ☞ CocoaPods →

    use_frameworks! ☞ Carthage ☞ Dependency management, iOS < 8.0 ☞ Include source files ☞ git submodules !
  20. Other Annoyances ☞ Quick open from Swift file ☞ NO

    REFACTORING!!!111elf !
  21. Future? Objective-C == techn. dept?