Skip to content

Instantly share code, notes, and snippets.

@dineshba
Last active May 30, 2018 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dineshba/0b6e6d8b8416ae74ccf65e3d72e42fc1 to your computer and use it in GitHub Desktop.
Save dineshba/0b6e6d8b8416ae74ccf65e3d72e42fc1 to your computer and use it in GitHub Desktop.
var anyValue: Any // If we don't know the type at compile time,
// we can use `Any`. It is mostly used for data
// coming from network which can be type casted as below.
anyValue = "string value" // assinging string value
anyValue is String // true
// type checking using `is` operator
if let stringValue = anyValue as? String { // if anyValue is castable to String, it will
print(stringValue.uppercased()) // store in stringValue and enter into `if-let`
}
anyValue = Programmer()
if let programmer = anyValue as? Programmer {
print(programmer.laptop)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment