Slide 1

Slide 1 text

A Swift Introduction to Swift Kaitlin Mahar

Slide 2

Slide 2 text

What makes Swift Swift?

Slide 3

Slide 3 text

Kaitlin Mahar Drivers Engineer @ MongoDB @k__mahar @kmahar

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Why build a Swift driver? MongoDB Mobile!

Slide 6

Slide 6 text

What is Swift, anyway?

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

So why should I use Swift?

Slide 11

Slide 11 text

Swift makes working with C libraries easy.

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Using OpaquePointers in MongoSwift

Slide 15

Slide 15 text

Using UnsafePointers in MongoSwift

Slide 16

Slide 16 text

Challenges of wrapping C

Slide 17

Slide 17 text

Swift has a powerful type system.

Slide 18

Slide 18 text

Built-in Optional Support

Slide 19

Slide 19 text

What is an optional?

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Using Optionals in MongoSwift

Slide 22

Slide 22 text

Using Optionals in MongoSwift

Slide 23

Slide 23 text

Using Optionals in MongoSwift

Slide 24

Slide 24 text

Extensions

Slide 25

Slide 25 text

Extensions Add new functionality to existing types

Slide 26

Slide 26 text

Extensions Add new functionality to existing types

Slide 27

Slide 27 text

Extensions Add new functionality to existing types

Slide 28

Slide 28 text

Protocols

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

ExpressibleByDictionaryLiteral

Slide 31

Slide 31 text

ExpressibleByDictionaryLiteral

Slide 32

Slide 32 text

ExpressibleByDictionaryLiteral

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Custom protocols

Slide 36

Slide 36 text

Custom types can conform to protocols

Slide 37

Slide 37 text

Native types can be extended to conform to protocols

Slide 38

Slide 38 text

Protocols can be used as types

Slide 39

Slide 39 text

Generics

Slide 40

Slide 40 text

Generics

Slide 41

Slide 41 text

Generics

Slide 42

Slide 42 text

Generics With Constraints

Slide 43

Slide 43 text

Using Generics in MongoSwift

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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!

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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)