Skip to content

Instantly share code, notes, and snippets.

@fitomad fitomad/typeof.swift
Created Feb 19, 2018

Embed
What would you like to do?
// 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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.