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. New Stuff ☞ guard ♥♥♥ ☞ #available() ♥ ☞ Protocol

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

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

    much indentation if let a = a, let b = b, let c = c { fn(a, b, c) }
  4. 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 }
  5. #available() let iOS9 = NSOperatingSystemVersion( majorVersion: 9, minorVersion: 0, patchVersion:

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

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

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

    { get } } extension Bookmarkable { public var remoteIdentifier: Int { return -1 } }
  9. 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) } }
  10. 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 }
  11. Error handling ☞ the new way: let contents: NSString? do

    { contents = try NSString(contentsOfFile: filePath, encoding: NSUTF8StringEncoding) } catch let error as NSError { print(error) }
  12. 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 } }
  13. Error handling do { try readFile(atPath: "~/.vim") } catch FileError.NoSuchFile

    { print("No such file") } catch FileError.IsDirectory { print("Item at path is a directory") }
  14. 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 }
  15. Mixed Projects ☞ Setup ☞ bridging Header (only app targets)

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

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

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

    use_frameworks! ☞ Carthage ☞ Dependency management, iOS < 8.0 ☞ Include source files ☞ git submodules !