Skip to content

Instantly share code, notes, and snippets.

@pepasflo
Created August 3, 2018 18:26
Show Gist options
  • Save pepasflo/2561e4ca7f43e5dc4640c8deab5bf7c6 to your computer and use it in GitHub Desktop.
Save pepasflo/2561e4ca7f43e5dc4640c8deab5bf7c6 to your computer and use it in GitHub Desktop.
I can store the type of an object in a dictionary, but I can't seem to match against it in a switch statement. Halp?
var dict: Dictionary<String, Any> = ["window.rootViewController": UIViewController.self]
switch dict["window.rootViewController"] {
// Expression pattern of type 'UIViewController.Type' cannot match values of type 'Any'
case .some(UIViewController.self):
print("yay!")
default:
print("boo!")
}
@pepasflo
Copy link
Author

pepasflo commented Aug 3, 2018

Thanks to Tim Vermeulen for the answer!

switch dict["window.rootViewController"] {
case let type? where type is UIViewController.Type:
    print("yay!")
default:
    print("boo!")
}

@illyabusigin
Copy link

import UIKit

var dict: Dictionary<String, Any> = ["window.rootViewController": UIViewController.self]

switch dict["window.rootViewController"] {
// Expression pattern of type 'UIViewController.Type' cannot match values of type 'Any'
case is UIViewController.Type:
    print("yay!")
default:
    print("boo!")
}

This works

@pepasflo
Copy link
Author

pepasflo commented Aug 3, 2018

Thanks @illyabusigin!

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