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

Everything you ever wanted to know about Swift but were afraid to ask!

Everything you ever wanted to know about Swift but were afraid to ask!

This presentation covers:
- Why developers should care about Swift.
- An overview of the languages features.
- A more detailed look at some of the features.
- The Swift sever work group.
- Kitura.

kilnerm

June 19, 2019
Tweet

Other Decks in Technology

Transcript

  1. Everything you ever wanted to know about Swift but were

    afraid to ask! Matt Kilner Linuxing in London - June 19th 2019
  2. Matt Kilner • Swift@IBM, Senior Software Engineer • Background in

    runtime systems: • Garbage Collector / Memory Manager • Runtime performance analysis • Full stack debugging • And a bit of Devops: • Terraform • Kubernetes • OpenStack [email protected] slack.kitura.io kilnerm
  3. Agenda • A brief history of Swift • Why should

    you be interested? • An overview of Swift language features • Common questions • Swift Server Work Group • Kitura • Q&A
  4. Swift: A brief history 2010 - Work within Apple begins

    09/2014 - V1.0 released 12/2015 - Open sourced on Github 03/2016 - V2.2 released First on Linux 03/2019- V5.0 released
  5. Swift: Use cases Where does Swift fit? Mobile Backend iOS

    team can own the backend. Backends can evolve to fit unique requirements
  6. Swift: Use cases Where does Swift fit? Mobile Backend iOS

    team can own the backend. Backends can evolve to fit unique requirements App requirements can be realised quicker
  7. Swift: Use cases Where does Swift fit? Mobile Backend Microservices

    iOS team can own the backend. Backends can evolve to fit unique requirements App requirements can be realised quicker
  8. Swift: Use cases Where does Swift fit? Mobile Backend Microservices

    iOS team can own the backend. Backends can evolve to fit unique requirements App requirements can be realised quicker Fast startup
  9. Swift: Use cases Where does Swift fit? Mobile Backend Microservices

    iOS team can own the backend. Backends can evolve to fit unique requirements App requirements can be realised quicker Fast startup Low memory
  10. Swift: Use cases Where does Swift fit? Mobile Backend Microservices

    iOS team can own the backend. Backends can evolve to fit unique requirements App requirements can be realised quicker Fast startup Low memory Interoperability
  11. Swift: language features Int Double Float Bool String Array Dictionary

    Set Constants… let Variables… var A brief overview of Swift
  12. Swift: language features Int Double Float Bool String Array Dictionary

    Set Constants… let Variables… var Type safety Type inference A brief overview of Swift
  13. Swift: language features Int Double Float Bool String Array Dictionary

    Set Constants… let Variables… var Type safety Type inference Tuples Optionals… var count: Int? nil A brief overview of Swift
  14. Swift: language features Int Double Float Bool String Array Dictionary

    Set Constants… let Variables… var Type safety Type inference Tuples Optionals… var count: Int? nil Preconditions Assertions A brief overview of Swift
  15. Swift: language features Closures Subscripts Inheritance Deinitialization Extensions Protocols Generics

    Opaque types ARC Memory safety Access control A brief overview of Swift
  16. Swift: Protocols What is an Optional? Optionals are used where

    a value may be absent and represent two possible states:
  17. Swift: Protocols What is an Optional? Optionals are used where

    a value may be absent and represent two possible states: A value exists and can be accessed by unwrapping. var name: String? = “Matt” if let name = name { print(name) }
  18. Swift: Protocols What is an Optional? Optionals are used where

    a value may be absent and represent two possible states: A value exists and can be accessed by unwrapping. No value exists var name: String? = “Matt” if let name = name { print(name) } var name: String? = nil
  19. Swift: Protocols What is optional chaining? Optional chaining is a

    process for querying and calling properties that may be nil.
  20. Swift: Protocols What is optional chaining? Optional chaining is a

    process for querying and calling properties that may be nil. If the optional property has a value the query or call is successful. If the property is nil the operation returns nil
  21. Swift: Protocols What is optional chaining? Optional chaining is a

    process for querying and calling properties that may be nil. If the optional property has a value the query or call is successful. If the property is nil the operation returns nil class Bike { var bell: Bell? }
  22. Swift: Protocols What is optional chaining? Optional chaining is a

    process for querying and calling properties that may be nil. If the optional property has a value the query or call is successful. If the property is nil the operation returns nil class Bike { var bell: Bell? } func ringBell() { guard let bell = self.bell else { return } bell.sound() }
  23. Swift: Protocols What is optional chaining? Optional chaining is a

    process for querying and calling properties that may be nil. If the optional property has a value the query or call is successful. If the property is nil the operation returns nil class Bike { var bell: Bell? } func ringBell() { guard let bell = self.bell else { return } bell.sound() } class Bike { var bell: Bell? } func ringBell() { self.bell?.sound() }
  24. Swift: Protocols What is a Protocol? Blueprint for a task

    or functionality: - Properties - Functions - Requirements
  25. Swift: Protocols What is a Protocol? Blueprint for a task

    or functionality: - Properties - Functions - Requirements If you want to be a thing then do: - This - That - The other
  26. Swift: Protocols When to use a Protocol? class MotorisedBike {

    var engineSize: Int func alert() { // Sound horn } } Inheritance
  27. Swift: Protocols When to use a Protocol? class MotorisedBike {

    var engineSize: Int func alert() { // Sound horn } } class Moped: MotorisedBike { var manufacturer: String } Inheritance
  28. Swift: Protocols When to use a Protocol? class MotorisedBike {

    var engineSize: Int func alert() { // Sound horn } } class Moped: MotorisedBike { var manufacturer: String } protocol Bike { func alert() } Inheritance Protocol
  29. Swift: Protocols When to use a Protocol? class MotorisedBike {

    var engineSize: Int func alert() { // Sound horn } } class Moped: MotorisedBike { var manufacturer: String } protocol Bike { func alert() } class Moped: Bike { func alert() { // Sound horn } } class Bicycle: Bike { func alert() { // Ring bell } } Inheritance Protocol
  30. Swift: Generic Types What is a generic type? A custom

    type that defines common behaviour to be applied to any other
  31. Swift: Generic Types What is a generic type? A custom

    type that defines common behaviour to be applied to any other struct IntStack { var stack = [Int]() mutating func push(_ item: Int) { stack.append(item) } mutating func pop() -> Int { return stack.removeLast() } }
  32. Swift: Generic Types What is a generic type? A custom

    type that defines common behaviour to be applied to any other struct IntStack { var stack = [Int]() mutating func push(_ item: Int) { stack.append(item) } mutating func pop() -> Int { return stack.removeLast() } } struct Stack<Element> { var stack = [Element]() mutating func push(_ item: Element) { stack.append(item) } mutating func pop() -> Element { return stack.removeLast() } }
  33. Swift: Generic Types What is a generic type? A custom

    type that defines common behaviour to be applied to any other struct IntStack { var stack = [Int]() mutating func push(_ item: Int) { stack.append(item) } mutating func pop() -> Int { return stack.removeLast() } } struct Stack<Element> { var stack = [Element]() mutating func push(_ item: Element) { stack.append(item) } mutating func pop() -> Element { return stack.removeLast() } } let intStack = Stack<Int>() let stringStack = Stack<String>()
  34. Swift: Generic Types Can I extend a generic type? extension

    Stack { var topItem: Element? { return stack.isEmpty ? nil : stack[stack.count - 1] } }
  35. Swift: Generic Types Can I extend a generic type? extension

    Stack { var topItem: Element? { return stack.isEmpty ? nil : stack[stack.count - 1] } } Can I constrain an extended a generic type?
  36. Swift: Generic Types Can I extend a generic type? extension

    Stack { var topItem: Element? { return stack.isEmpty ? nil : stack[stack.count - 1] } } Can I constrain an extended a generic type? extension Stack where Element: Equatable { func isTop(_ item: Element) -> Bool { guard let topItem = stack.last else { return false } return topItem == item } }
  37. Swift: Subscripts When is a Subscript useful? struct SudokuGrid {

    let rows: Int = 9 let colums: Int = 9 var grid: [Int] = 9 init() {
 grid = Array(repeating: 0, count: rows * columns) } func gridLocationIsValid(for row: Int, and column: Int) -> Bool { // Validation code } }
  38. Swift: Subscripts When is a Subscript useful? struct SudokuGrid {

    let rows: Int = 9 let colums: Int = 9 var grid: [Int] = 9 init() {
 grid = Array(repeating: 0, count: rows * columns) } func gridLocationIsValid(for row: Int, and column: Int) -> Bool { // Validation code } } subscript(row: Int, column: Int) -> Int { get { // assert location valid return grid[(row * columns) + column] } set { // assert location valid grid[(row * columns) + column] = newValue } }
  39. Swift: Subscripts When is a Subscript useful? struct SudokuGrid {

    let rows: Int = 9 let colums: Int = 9 var grid: [Int] = 9 init() {
 grid = Array(repeating: 0, count: rows * columns) } func gridLocationIsValid(for row: Int, and column: Int) -> Bool { // Validation code } } subscript(row: Int, column: Int) -> Int { get { // assert location valid return grid[(row * columns) + column] } set { // assert location valid grid[(row * columns) + column] = newValue } } var myGrid = SudokuGrid() myGrid[0,8] = 5 var value = myGrid[0,8] // 5
  40. Swift: Subscripts Are Subscripts just for instances? enum DayOfWeek: Int

    { case sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday static subscript(n: Int) -> DayOfWeek { return DayOfWeek(rawValue: n)! } }
  41. Swift: Subscripts Are Subscripts just for instances? enum DayOfWeek: Int

    { case sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday static subscript(n: Int) -> DayOfWeek { return DayOfWeek(rawValue: n)! } } let monday = DayOfWeek[2] print(monday)
  42. Swift: Subscripts Are Subscripts just for instances? enum DayOfWeek: Int

    { case sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday static subscript(n: Int) -> DayOfWeek { return DayOfWeek(rawValue: n)! } } let monday = DayOfWeek[2] print(monday) From Swift 5.1 !!
  43. Swift: Swift Server Work Group What is the Swift Server

    Work Group? A group that promotes the use of Swift for developing and deploying server applications
  44. Swift: Swift Server Work Group What is the Swift Server

    Work Group? A group that promotes the use of Swift for developing and deploying server applications Members consist of: - Apple - IBM Kitura team - Vapor team
  45. Swift: Swift Server Work Group What is the Swift Server

    Work Group? A group that promotes the use of Swift for developing and deploying server applications Members consist of: - Apple - IBM Kitura team - Vapor team Uses the Swift Server Forum for general discussion
  46. Swift: Swift Server Work Group What is the Swift Server

    Work Group? A group that promotes the use of Swift for developing and deploying server applications Members consist of: - Apple - IBM Kitura team - Vapor team Uses the Swift Server Forum for general discussion What does the Swift Server Work Group do?
  47. Swift: Swift Server Work Group What is the Swift Server

    Work Group? A group that promotes the use of Swift for developing and deploying server applications Members consist of: - Apple - IBM Kitura team - Vapor team Uses the Swift Server Forum for general discussion What does the Swift Server Work Group do? Defines and prioritises efforts that address the needs of the Swift Server community.
  48. Swift: Swift Server Work Group What is the Swift Server

    Work Group? A group that promotes the use of Swift for developing and deploying server applications Members consist of: - Apple - IBM Kitura team - Vapor team Uses the Swift Server Forum for general discussion What does the Swift Server Work Group do? Defines and prioritises efforts that address the needs of the Swift Server community. Runs an incubation process intended to reduce duplication of effort and promote compatibility and good practice.
  49. Swift: Swift Server Work Group What is the Swift Server

    Work Group? A group that promotes the use of Swift for developing and deploying server applications Members consist of: - Apple - IBM Kitura team - Vapor team Uses the Swift Server Forum for general discussion What does the Swift Server Work Group do? Defines and prioritises efforts that address the needs of the Swift Server community. Runs an incubation process intended to reduce duplication of effort and promote compatibility and good practice. Provides feedback on language features needed by the server community to the Swift Core team.