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 full-size slide

  2. What makes Swift Swift?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. Why build a Swift driver?
    MongoDB Mobile!

    View full-size slide

  6. What is Swift, anyway?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. So why should I use Swift?

    View full-size slide

  11. Swift makes working with
    C libraries easy.

    View full-size slide

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

    View full-size 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 full-size slide

  14. Using OpaquePointers in MongoSwift

    View full-size slide

  15. Using UnsafePointers in MongoSwift

    View full-size slide

  16. Challenges of wrapping C

    View full-size slide

  17. Swift has a powerful
    type system.

    View full-size slide

  18. Built-in Optional
    Support

    View full-size slide

  19. What is an optional?

    View full-size slide

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

    View full-size slide

  21. Using Optionals in MongoSwift

    View full-size slide

  22. Using Optionals in MongoSwift

    View full-size slide

  23. Using Optionals in MongoSwift

    View full-size slide

  24. Extensions
    Add new functionality to existing types

    View full-size slide

  25. Extensions
    Add new functionality to existing types

    View full-size slide

  26. Extensions
    Add new functionality to existing types

    View full-size slide

  27. 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 full-size slide

  28. ExpressibleByDictionaryLiteral

    View full-size slide

  29. ExpressibleByDictionaryLiteral

    View full-size slide

  30. ExpressibleByDictionaryLiteral

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. Custom protocols

    View full-size slide

  34. Custom types can conform to protocols

    View full-size slide

  35. Native types can be extended to conform to protocols

    View full-size slide

  36. Protocols can be used as types

    View full-size slide

  37. Generics With Constraints

    View full-size slide

  38. Using Generics in MongoSwift

    View full-size slide

  39. 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 full-size slide

  40. 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 full-size slide

  41. 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 full-size slide

  42. 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 full-size slide