Type_Casting

Type Casting

Forced type casting (as!)

Conditional type casting (as?)

Type Casting

medium | type-casting-in-swift

how-to-convert-a-string-to-a-custom-type-in-swift

Dictionary Types Casting

Converting from [String: Any] => [String: String]
Using LosslessStringConvertible in

  • Bool
  • Double
  • Float
  • Float80
  • Path
  • Substring
  • Unicode.Scalar

String(describing: CustomStringConvertible)

let people: [String: Any] = ["Taylor": 178.0, "Justin": 175, "Ed": 173.23, "Nick": true, "Lexton": "Son"]
let peopleValue = people.mapValues { String(describing: $0) }

// ["Nick": "true", "Taylor": "178.0", "Justin": "175", "Ed": "173.23", "Lexton": "Son"]

converting-string-any-to-string-string

Existential any

Converting to Data

let cafe: Data? = "Café".data(using: .utf8) // non-nil

string-to-data-and-back

medium | JSON data Dictionary parsing