Skip to content

Instantly share code, notes, and snippets.

@ygweric
Created September 22, 2017 22:52
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 ygweric/325fdffc4d34091e58fd364ac7bdcbbd to your computer and use it in GitHub Desktop.
Save ygweric/325fdffc4d34091e58fd364ac7bdcbbd to your computer and use it in GitHub Desktop.
import UIKit
func isExactClass<T>(obj: Any, _ _: T.Type) -> Bool{
return type(of: obj) == T.self
}
class Dog {
}
class Wolf: Dog {
}
let dog = Dog()
let wolf = Wolf()
wolf.self === Dog.self //false
dog.self === Dog.self //false
type(of: dog) == Dog.self //true
type(of: wolf) == Dog.self //false
isExactClass(obj: dog, Dog.self) //true
isExactClass(obj: wolf, Dog.self) //false
var x: Any.Type
x = Dog.self
type(of: dog) == x // true
type(of: x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment