Type Casting

Download swift eBook

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"]]

Related Examples

Stats

92 Contributors: 5
Friday, January 27, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook