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

Emerging Best Practices in Swift

0ebf471a3ae8df42a84f93a7efbbdbd0?s=47 Ash Furrow
September 14, 2015

Emerging Best Practices in Swift

0ebf471a3ae8df42a84f93a7efbbdbd0?s=128

Ash Furrow

September 14, 2015
Tweet

Transcript

  1. Emerging Best Practices in Swift Ash Furrow

  2. None
  3. I was Afraid That we'd just write Objective-C in Swift

    syntax.
  4. Everything turned out Fine

  5. Today, we're exploring best practices in Swift.

  6. We've been here before. Swift 2 is significantly different. Always

    be learning.
  7. Let's Go

  8. Those who don't know history are doomed to repeat it.

    — Lots of people.
  9. Wrong.

  10. Those who don't know the past can't make informed decisions

    about the present. — Me
  11. iOS 5 or Earlier? Let's see a show of hands.

  12. Before Object Literals NSArray *array = [NSArray arrayWithObjects: @"This", @"is",

    @"so", @"tedious", nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"Who would do this?", @"Not me", nil]; NSNumber *number = [NSNumber numberWithInt: 401];
  13. Before Object Literals and ARC NSArray *array = [[NSArray arrayWithObjects:

    @"This", @"is", @"so", @"tedious", nil] retain]; NSDictionary *dictionary = [[NSDictionary dictionaryWithObjectsAndKeys: @"Who would do this?", @"Not me", nil] retain]; NSNumber *number = [[NSNumber numberWithInt: 401] retain];
  14. !

  15. After Object Literals NSArray *array = @[ @"This", @"is", @"much",

    @"better" ]; NSDictionary *dictionary = @{ @"Who likes this?": @"Me!" }; NSNumber *number = @(401);
  16. Adopted immediately. Clearly better. Became a best practice.

  17. Blocks — iOS 4 introduced blocks and GCD. — Adopted...

    eventually. — Required new ideas. — Became a best practice.
  18. Blocks — iOS 4 introduced blocks and GCD. — Adopted...

    eventually. — Required new ideas. — Became a best practice. — Blocks now enable other best practices.
  19. Swift 2

  20. Swift 2 — Lots of new syntax. — New syntax

    lets us do cool new things. — Like blocks, syntax is only a tool.
  21. Swift 2 — guard — defer — throws — etc...

  22. Should I use guard?

  23. What can I do with guard?

  24. Examples

  25. If Overload if let thing = optionalThing { if thing.shouldDoThing

    { if let otherThing = thing.otherThing { doStuffWithThing(otherThing) } } }
  26. where to the rescue if let thing = optionalThing, let

    otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) }
  27. where to the rescue if let thing = optionalThing, let

    otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) } if...where isn't cool. You know what's cool?
  28. where to the rescue if let thing = optionalThing, let

    otherThing = thing.otherThing where thing.shoudDoThing { doStuffWithThing(otherThing) } if...where isn't cool. You know what's cool? ... Neither do I. Let's look together!
  29. Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String]

    { var mutableArray = [String]() for string in parameter { if string.hasPrefix(prefix) { mutableArray.append(string) } } return mutableArray }
  30. Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String]

    { var mutableArray = [String]() for string in parameter { if string.hasPrefix(prefix) { mutableArray.append(string) } } return mutableArray } That's silly.
  31. Avoid mutability func strings(parameter: [String], startingWith prefix: String) -> [String]

    { return parameter.filter { $0.hasPrefix(prefix) } }
  32. Extract associated values 1. Use Swift enums. 2. Attach associated

    values. 3. Extract using case.
  33. Extract associated values enum Result<T> { case Success(T) case Failure(reason:

    String) } ... switch doThing() { case .Success: print("!") case .Failure(let reason): print("Oops: \(reason)") }
  34. Extract associated values if case .Success = doThing() { print("!")

    }
  35. That's all just syntax.

  36. Protocol-Oriented Programming

  37. Just go watch the WWDC video.

  38. Syntax itself is not a best practice. The patterns enabled

    by syntax are what really matter. We need to discover them.
  39. Learning !

  40. Learning shouldn't just happen during the Xcode betas.

  41. Learning is a constant activity, a state of mind.

  42. Learning — Look for code smells. — Ask yourself how

    you'd solve something differently. — Pick a Swift feature, ask "what could I do with this?" — Be comfortable throwing code away.
  43. What about other communities? I bet they have good ideas,

    too...
  44. You should write a Blog

  45. Wrap-up

  46. We have a history of being awesome, let's keep it

    up. Re-evaluate solutions to familiar problems. Always be learning. Also, write a blog.
  47. Make better mistakes tomorrow.