$30 off During Our Annual Pro Sale. View Details »

Solving Problems the Swift Way

Solving Problems the Swift Way

A presentation on solving problems in idiomatic Swift.

Ash Furrow

July 05, 2014
Tweet

More Decks by Ash Furrow

Other Decks in Technology

Transcript

  1. Idiomatic Swift
    Ash Furrow
    @ashfurrow

    View Slide

  2. View Slide

  3. 1.Better ways to solve familiar problems using
    Swift
    2.Everyone is a beginner again
    3.We should share what we learn

    View Slide

  4. Problem-Solving

    View Slide

  5. You are here You wanna be here
    “Problem Solving”

    View Slide

  6. • It would be a shame not to take advantage of
    these new tools and techniques
    • Let’s take a look at some examples

    View Slide

  7. • Completely new concept of nil
    • Indicates “missing” value
    • Replaces nil, Nil, NULL, CGRectNull, -1,
    NSNotFound, NSNull, etc
    • Haskell’s “Maybe” type
    • C#’s “Nullable Types”
    Optionals

    View Slide

  8. • Works well with Swift’s compile-time type safety
    • Which is awesome
    • No, seriously, awesome
    • Eliminates several classes of bugs
    • Don’t over-use optional types
    Optionals

    View Slide

  9. let a = someFunction() //returns Int?

    if a != nil {
    // use a!
    }
    Optionals

    View Slide

  10. let a = someFunction() //returns Int?

    if let b = a {

    // do something with b

    }



    if let a = a {

    // do something with a

    }
    Optionals

    View Slide

  11. • Tuples are compound values
    • They are lightweight, temporary containers
    for multiple values
    • Those values can be named
    • Useful for functions with multiple return types
    Tuples

    View Slide

  12. func calculate() -> (Bool, Int?) {
    // ...
    return (result, errorCode)
    }
    Tuples

    View Slide

  13. func calculate() -> (Bool, Int?) {
    // ...
    return (result, errorCode)
    }
    !
    let calculation = calculate()
    !
    if (calculation.0) {
    // …
    }
    Tuples

    View Slide

  14. func calculate() -> (Bool, Int?) {
    // ...
    return (result, errorCode)
    }
    !
    let calculation = calculate()
    let (result, _) = calculation
    !
    if (result) {
    // …
    }
    Tuples

    View Slide

  15. func calculate() -> (result: Bool, errorCode: Int?) {
    // ...
    return (result: result, errorCode: errorCode)
    }
    !
    let calculation = calculate()
    if (calculation.errorCode) {
    // ...
    }
    Tuples

    View Slide

  16. for (key, value) in dictionary {
    // ...
    }
    Tuples

    View Slide

  17. • New APIs shouldn’t use out parameters
    • eg: NSError pointers
    • Really great for use in pattern-matching
    Tuples

    View Slide

  18. • Borrowed from functional programming
    • Really useful in tail-recursive functions
    • Like “switch” statements on steroids
    Pattern-Matching

    View Slide

  19. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
    (NSIndexPath *)indexPath {
    switch (indexPath.section) {
    case 0:
    {
    switch (indexPath.row) {
    case 0:
    ...

    }
    }
    break;

    }
    }
    Pattern-Matching

    View Slide

  20. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
    (NSIndexPath *)indexPath {
    switch (indexPath.section) {
    case ASHLoginSection:
    {
    switch (indexPath.row) {
    case ASHLoginSectionUserNameRow:
    ...

    }
    }
    break;

    }
    }
    Pattern-Matching

    View Slide

  21. override func tableView(tableView: UITableView!,
    didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    switch (indexPath.section, indexPath.row) {
    case (0, _):
    ...
    default:
    ...
    }
    }
    Pattern-Matching

    View Slide

  22. override func tableView(tableView: UITableView!,
    didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    switch (indexPath.section, indexPath.row) {
    case (0, let row):
    ...
    default:
    ...
    }
    }
    Pattern-Matching

    View Slide

  23. override func tableView(tableView: UITableView!,
    didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    switch (indexPath.section, indexPath.row) {
    case (0, let row) where row > 5:
    ...
    default:
    ...
    }
    }
    Pattern-Matching

    View Slide

  24. struct IntList {
    var head: Int = 0
    var tail: IntList?
    }
    !
    ...
    !
    switch (list.head, list.tail) {
    case (let head, nil):
    //...
    case (let head, let tail):
    //...
    }
    Pattern-Matching

    View Slide

  25. • Generics are common in other languages,
    like C# and C++
    • Using a generic type as a placeholder, we
    can infer the type of variables at compile-
    time
    • A part of Swift’s “safe by default” behaviour
    Generics

    View Slide

  26. struct Stack {
    var items = [T]()
    mutating func push(item: T) {
    items.append(item)
    }
    mutating func pop() -> T {
    return items.removeLast()
    }
    }
    Generics

    View Slide

  27. var stack = Stack()
    !
    var stack = Stack()
    !
    var stack = Stack()
    Generics

    View Slide

  28. struct Stack : Equatable {
    var items = [T]()
    mutating func push(item: T) {
    items.append(item)
    }
    mutating func pop() -> T {
    return items.removeLast()
    }
    }
    !
    func ==(lhs: Stack, rhs: Stack) -> Bool {
    return lhs.items == rhs.items
    }
    Generics

    View Slide

  29. • Use stacks whenever you want to define an
    abstract data type structure
    • Whenever possible, don’t bind new data
    structures to existing ones
    • Use protocols for loose coupling
    Generics

    View Slide

  30. • Optionals
    • Pattern-matching
    • Tuples
    • Generics

    View Slide

  31. Everyone is a Beginner

    View Slide

  32. • No one is an expert in Swift
    • This can be kind of stressful
    • Relax
    Everyone is a Beginner

    View Slide

  33. • The benefits outweighs the cost of learning
    • Depending on your circumstance
    • Have your say
    Everyone is a Beginner

    View Slide

  34. • The hardest thing is the most important thing
    • Start
    Everyone is a Beginner

    View Slide

  35. • Don’t be embarrassed to ask questions!
    • Try to ask in public so others can benefit
    from the answer
    Everyone is a Beginner

    View Slide

  36. • Let’s borrow ideas
    Everyone is a Beginner

    View Slide

  37. • Community-based conventions and
    guidelines are still being established
    Everyone is a Beginner

    View Slide

  38. We Should Share 

    What We Learn

    View Slide

  39. • Conventions and guidelines are still in flux
    • There’s an opportunity to significantly alter
    the future of iOS and OS X programming
    We Should Share
    What We Learn

    View Slide

  40. • The demand for material on Swift is HUGE
    • Great opportunity to get known
    We Should Share
    What We Learn

    View Slide

  41. • When you teach, you learn
    We Should Share
    What We Learn

    View Slide

  42. • If we all share what we learn, we all get
    smarter
    • Rising tides lift all boats
    We Should Share
    What We Learn

    View Slide

  43. • Stack Overflow
    • Blogs
    • Tweets
    • Gists
    • Open source
    • Radars
    We Should Share
    What We Learn

    View Slide

  44. http://github.com/artsy/eidolon

    View Slide

  45. 1.Better ways to solve familiar problems using
    Swift
    2.Everyone is a beginner again
    3.We should share what we learn

    View Slide

  46. Let’s Make Better
    Mistakes Tomorrow

    View Slide

  47. Thank you"
    @ashfurrow

    View Slide