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

More Decks by Kaitlin Mahar

Other Decks in Programming


  1. A Swift Introduction to Swift Kaitlin Mahar

  2. What makes Swift Swift?

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

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

    BSON 00101...
  5. Why build a Swift driver? MongoDB Mobile!

  6. What is Swift, anyway?

  7. What is Swift? • released in 2014, open-sourced in 2015

    • up to version 4.1.2
  8. Why did Apple create Swift? “Objective-C without the C”

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

    community breaking changes
  10. So why should I use Swift?

  11. Swift makes working with C libraries easy.

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

    Swift driver C driver
  13. Descriptive Pointer Types OpaquePointer UnsafePointer<T> UnsafeMutablePointer<T> UnsafeBufferPointer<T> UnsafeMutableBufferPointer<T> UnsafeRawPointer UnsafeMutableRawPointer

    UnsafeRawBufferPointer UnsafeMutableRawBufferPointer Opaque (or not) <T> or Raw Mutable (or not) Buffer (or not)
  14. Using OpaquePointers in MongoSwift

  15. Using UnsafePointers in MongoSwift

  16. Challenges of wrapping C

  17. Swift has a powerful type system.

  18. Built-in Optional Support

  19. What is an optional?

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

  21. Using Optionals in MongoSwift

  22. Using Optionals in MongoSwift

  23. Using Optionals in MongoSwift

  24. Extensions

  25. Extensions Add new functionality to existing types

  26. Extensions Add new functionality to existing types

  27. Extensions Add new functionality to existing types

  28. Protocols

  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
  30. ExpressibleByDictionaryLiteral

  31. ExpressibleByDictionaryLiteral

  32. ExpressibleByDictionaryLiteral

  33. How to represent BSON values? Double String Document Array Binary

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

    ObjectId Boolean Date Regex CodeWithScope Int32 Timestamp Int64 Decimal128 MinKey MaxKey
  35. Custom protocols

  36. Custom types can conform to protocols

  37. Native types can be extended to conform to protocols

  38. Protocols can be used as types

  39. Generics

  40. Generics

  41. Generics

  42. Generics With Constraints

  43. Using Generics in MongoSwift

  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?)
  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!
  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
  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)