Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created February 19, 2018 10:05
Show Gist options
  • Save fitomad/2d51e3cfeb39a4667ae48b9cac631cb3 to your computer and use it in GitHub Desktop.
Save fitomad/2d51e3cfeb39a4667ae48b9cac631cb3 to your computer and use it in GitHub Desktop.
// An array with Int, Double and String types inside
let items: [Any] = [ 32, 32.0, "32" ]
for item in items
{
switch type(of: item)
{
case is Int.Type:
print("Type Int: \(item)")
case is Double.Type:
print("Type Double: \(item)")
case is String.Type:
print("Type String: \(item)")
default:
print("No sé que es")
}
}
/*
Results:
---
Type Int: 32
Type Double: 32.0
Type String: 32
*/
@fitomad
Copy link
Author

fitomad commented Feb 19, 2018

A different way to chech a var type in Swift.
Include another way to use de switch control flow statement (case is)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment