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

A Swift Introduction to Swift

A Swift Introduction to Swift

Swift has been among StackOverflow’s “most loved” languages every year since its 2014 release. With examples from the MongoDB Swift driver, this talk will highlight unique, powerful features of Swift, and discuss the challenges of writing good code in a young, flexible and rapidly changing language. Attendees will come away informed about some of the advantages and challenges of writing code in Swift. They will be better prepared to write idiomatic Swift of their own, and will have a deeper understanding of how a MongoDB driver works.

Kaitlin Mahar

June 26, 2018
Tweet

More Decks by Kaitlin Mahar

Other Decks in Programming

Transcript

  1. A Swift Introduction to Swift
    Kaitlin Mahar

    View Slide

  2. What makes Swift Swift?

    View Slide

  3. Kaitlin Mahar
    Drivers Engineer @ MongoDB
    @k__mahar @kmahar

    View Slide

  4. What is a MongoDB driver?
    app driver MongoDB
    … !!
    BSON
    00101...

    View Slide

  5. Why build a Swift driver?
    MongoDB Mobile!

    View Slide

  6. What is Swift, anyway?

    View Slide

  7. What is Swift?
    • released in 2014, open-sourced in 2015
    • up to version 4.1.2

    View Slide

  8. Why did Apple create Swift?
    “Objective-C without the C”

    View Slide

  9. So why do people still use Objective-C?
    young language small community breaking changes

    View Slide

  10. So why should I use Swift?

    View Slide

  11. Swift makes working with
    C libraries easy.

    View Slide

  12. The Swift driver wraps the C driver.
    Swift
    app
    MongoDB
    Swift driver
    C driver

    View Slide

  13. Descriptive Pointer Types
    OpaquePointer
    UnsafePointer
    UnsafeMutablePointer
    UnsafeBufferPointer
    UnsafeMutableBufferPointer
    UnsafeRawPointer
    UnsafeMutableRawPointer
    UnsafeRawBufferPointer
    UnsafeMutableRawBufferPointer
    Opaque (or not)
    or Raw
    Mutable (or not)
    Buffer (or not)

    View Slide

  14. Using OpaquePointers in MongoSwift

    View Slide

  15. Using UnsafePointers in MongoSwift

    View Slide

  16. Challenges of wrapping C

    View Slide

  17. Swift has a powerful
    type system.

    View Slide

  18. Built-in Optional
    Support

    View Slide

  19. What is an optional?

    View Slide

  20. What is an optional?
    A typed value that is optionally set

    View Slide

  21. Using Optionals in MongoSwift

    View Slide

  22. Using Optionals in MongoSwift

    View Slide

  23. Using Optionals in MongoSwift

    View Slide

  24. Extensions

    View Slide

  25. Extensions
    Add new functionality to existing types

    View Slide

  26. Extensions
    Add new functionality to existing types

    View Slide

  27. Extensions
    Add new functionality to existing types

    View Slide

  28. Protocols

    View Slide

  29. Protocols
    • Similar to an interface in other languages -
    specify a required set of properties and methods
    • A type that implement those requirements
    conforms to the protocol
    • Classes, structs, and enums can all conform

    View Slide

  30. ExpressibleByDictionaryLiteral

    View Slide

  31. ExpressibleByDictionaryLiteral

    View Slide

  32. ExpressibleByDictionaryLiteral

    View Slide

  33. How to represent BSON values?
    Double String
    Document Array
    Binary ObjectId
    Boolean Date
    Regex CodeWithScope
    Int32 Timestamp
    Int64 Decimal128
    MinKey MaxKey

    View Slide

  34. How to represent BSON values?
    Double String
    Document Array
    Binary ObjectId
    Boolean Date
    Regex CodeWithScope
    Int32 Timestamp
    Int64 Decimal128
    MinKey MaxKey

    View Slide

  35. Custom protocols

    View Slide

  36. Custom types can conform to protocols

    View Slide

  37. Native types can be extended to conform to protocols

    View Slide

  38. Protocols can be used as types

    View Slide

  39. Generics

    View Slide

  40. Generics

    View Slide

  41. Generics

    View Slide

  42. Generics With Constraints

    View Slide

  43. Using Generics in MongoSwift

    View Slide

  44. Challenges of Swift’s type system
    • Lacking some desired features
    • Auto-generated ==, hashing (new in 4.1)
    • Conditional conformance (new in 4.1.. ish)
    • Default type parameters (someday?)

    View Slide

  45. In summary…
    • Swift is new and exciting!
    • Swift has a powerful type system: protocols,
    extensions, generics, optionals, and more.
    • Swift interoperates easily with C.
    • You should try writing Swift!

    View Slide

  46. What’s next for the driver?
    • 1.0 release
    • Full CRUD API
    • 4.0 features
    • replica sets, etc.
    • Integration with Swift web frameworks
    • Replace libbson usage with native Swift

    View Slide

  47. Want to learn more?
    Today:
    3:30-4pm MongoDB Mobile session in Gibson
    On your own:
    The Swift Programming Language (by Apple, free)
    objc.io videos and books
    Swift by Sundell (blog)

    View Slide