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)