class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let a = A()
let b = B()
printAOrBText(a)
printAOrBText(b)
printAOrBText(A.self)
printAOrBText(B.self)
printAOrBText1(B.self)
}
}
class A {
class var text: String {
return "A text"
}
}
class B: A {
override class var text: String {
return "B text"
}
}
func printAOrBText(_ value: A) {
let typetext = type(of: value)
print(typetext.text)
}
func printAOrBText(_ value: A.Type) {
print(value.text)
}
func printAOrBText1(_ value: AnyObject.Type) {
if value == A.self {
print(A.text)
} else {
print("error")
}
}
打印结果
A text
B text
A text
B text
error