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

Swift Solutions

Swift Solutions

In this talk we start by looking at where we came from with Objective-C, and how we feel about Swift. This presentation is about trying to discover what idiomatic Swift is by looking at a series of examples with "Swift" solutions.

Ben Scheirman

April 10, 2015
Tweet

More Decks by Ben Scheirman

Other Decks in Programming

Transcript

  1. [ ]

  2. C#

  3. NSFileManager *fm = [NSFileManager defaultManager]; NSError *error = nil; BOOL

    result = [fm removeItemAtPath:path error:&error]; if (!result) { NSLog(“The error was: %@“, [error localizedDescription]); }
  4. !

  5. no?

  6. hashOut.data = hashes + SSL_MD5_DIGEST_LEN; hashOut.length = SSL_SHA1_DIGEST_LEN; if ((err

    = SSLFreeBuffer(&hashCtx)) != 0) goto fail; if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail; if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0) goto fail; err = sslRawVerify(...); . . .
  7. hashOut.data = hashes + SSL_MD5_DIGEST_LEN; hashOut.length = SSL_SHA1_DIGEST_LEN; if ((err

    = SSLFreeBuffer(&hashCtx)) != 0) goto fail; if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail; <---------- if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0) goto fail; err = sslRawVerify(...); . . .
  8. func viewDidLoad() { super.viewDidLoad() let config = NSURLSessionConfiguration.defaultSessionConfiguration() session =

    NSURLSession( configuration: config, delegate: self, delegateQueue: nil) navigationItem.leftBarButtonItem = UIBarButtonItem( barButtonSystemItem: .Bookmarks, target: self, action: "bookmarks:") navigationItem.rightBarButtonItem = UIBarButtonItem( barButtonSystemItem: .Add, target: self, action: "add:") ... }
  9. @interface MyViewController () @property (nonatomic, strong) NSURLSessionConfiguration *configuration; @end -

    (NSURLSessionConfiguration *)configuration { if (_configuration == nil) { _configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; } return _configuration; }
  10. lazy var configuration = NSURLSessionConfiguration.defaultSessionConfiguration() lazy var session: NSURLSession =

    { return NSURLSession(configuration: self.configuration, delegate: self, delegateQueue: nil) }()
  11. lazy var configuration = NSURLSessionConfiguration.defaultSessionConfiguration() lazy var session: NSURLSession =

    { return NSURLSession(configuration: self.configuration, delegate: self, delegateQueue: nil) }() lazy var leftBarButtonItem: UIBarButtonItem = { return UIBarButtonItem( barButtonSystemItem: .Bookmarks, target: self, action: "bookmarks:") }() lazy var rightBarButtonItem: UIBarButtonItem = { return UIBarButtonItem( barButtonSystemItem: .Add, target: self, action: "add:") }()
  12. func viewDidLoad() { super.viewDidLoad() let config = NSURLSessionConfiguration.defaultSessionConfiguration() session =

    NSURLSession( configuration: config, delegate: self, delegateQueue: nil) navigationItem.leftBarButtonItem = UIBarButtonItem( barButtonSystemItem: .Bookmarks, target: self, action: "bookmarks:") navigationItem.rightBarButtonItem = UIBarButtonItem( barButtonSystemItem: .Add, target: self, action: "add:") ... }
  13. let (object, error) = JSONObjectWithData(data) if let someObject = object

    { // can work with someObject } else { // error }
  14. switch(result) { case (.Some(let x), nil): println("x's type is AnyObject")

    case (nil, .Some(let e)): println("e's type is NSError") default: }
  15. func JSONObjectWithData( data: NSData, options: NSJSONReadingOptions = nil ) ->

    Result<AnyObject> { .... if let error = parserError { return .Error(error) } else { return .Value(parsedObject) } }
  16. let result = JSONObjectWithData(data) switch(result) { case .Value(let obj): println("we

    parsed the object: \(obj)") case .Error(let error): println("error parsing json: \(error)") }
  17. enum Result<T> { case Value(Box<T>) case Error(NSError) } switch result

    { case .Value(let box): let val = box.unbox ... }
  18. Given&a&Shopping&cart&full&of&items... let shoppingCart = [ Item(name: "Apples", price: 0.67, taxable:

    false), Item(name: "Bananas", price: 1.87, taxable: false), Item(name: "Beer", price: 6.99, taxable: true), Item(name: "Candy", price: 4.00, taxable: true), Item(name: "Ice Cream", price: 6.00, taxable: true) ]
  19. let TaxRate = 0.0825 var tax = 0.0 for item

    in shoppingCart { } println(tax)
  20. let TaxRate = 0.0825 var tax = 0.0 for item

    in shoppingCart { if item.taxable { } } println(tax)
  21. let TaxRate = 0.0825 var tax = 0.0 for item

    in shoppingCart { if item.taxable { tax += item.price * TaxRate } } println(tax)
  22. map

  23. let items = [1, 2, 3] map(items, { (item: Int)

    -> Int in return item * 2 })
  24. func reduce<S : SequenceType, U>( sequence: S, initial: U, combine:

    @noescape (U, S.Generator.Element) -> U) -> U
  25. Reduce&to&a&single&value&(the&amount&of&tax) let taxable = shoppingCart.filter { $0.taxable } let taxableAmounts

    = taxable.map { $0.price } let tax = taxableAmounts.reduce(0) { (tax, itemPrice) in return tax + (itemPrice * TaxRate) }
  26. Can$simplify$the$reduce$block... let taxable = shoppingCart.filter { $0.taxable } let taxableAmounts

    = taxable.map { $0.price } let itemTaxes = taxable.map { $0 * TaxRate } let tax = itemTaxes.reduce(0) { (tax, itemTax) in return tax + (itemTax) }
  27. Can$just$use$the$+$func-on... let taxable = shoppingCart.filter { $0.taxable } let taxableAmounts

    = taxable.map { $0.price } let itemTaxes = taxable.map { $0 * TaxRate } let tax = itemTaxes.reduce(0, combine: +)
  28. ..."and"chain"them"together... let tax = shoppingCart .filter { $0.taxable } .map

    { $0.price } .map { $0 * TaxRate } .reduce(0, combine: +) println(tax) // 1.40
  29. func taxFunction(rate: Double) -> ([Item]) -> Double { return {

    (items) in return items .filter { $0.taxable } .map { $0.price } .map { $0 * rate } .reduce(0, combine: +) } }
  30. There%is%no%fla,en%func0on,%but%we%could% write%one... func flatten<T>(array: [[T]) -> [T] { var result

    = [T]() for item in array { for subItem in item { result.append(subItem) } } return result }
  31. enum Result<T> { case Value(Box<T>) case Error(NSError) func flatMap<U>(next: T

    -> Result<U>) -> Result<U> { switch self { case .Value(let box): let val = box.unbox return next(val) case .Error(let err): return .Error(err) } } }
  32. func tableView(tableView: UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell { if indexPath.section

    == 0 { if indexPath.row == 0 { // } else { // ... } } else if indexPath.section == numberOfSectionsInTableView(tableView) - 1 { // ... } else { // ... } }
  33. func tableView(tableView: UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell { switch (indexPath)

    { case (0, 0): ... case (0, let row): ... case (let section, let row) where isLastSection(section): ... case (let section, let row): ... } }
  34. enum ApiEndpoints { case Notifications case UserProfile(String) case ProductDetail(Int, Int)

    var path: String { switch(self) { case .Notifications: return "/notifications" case .UserProfile(let username): return "/users/\(username.stringByAddingPercentEscapes...)" case .ProductDetail(let categoryId, let productId): return "/categories/\(categoryId)/products/\(productId)" } } }
  35. !