Swift LanguageType Casting


Syntax

  • let name = json["name"] as? String ?? "" // Output: john

  • let name = json["name"] as? String // Output: Optional("john")

  • let name = rank as? Int // Output: Optional(1)

  • let name = rank as? Int ?? 0 // Output: 1

  • let name = dictionary as? [String: Any] ?? [:] // Output: ["name" : "john", "subjects": ["Maths", "Science", "English", "C Language"]]