在Swift中,我们不能再使用"self.class“。
我想为NSObject的子类中的描述方法打印自己的类名。
我怎么打印它呢?或者我应该创建一个新的方法来获取类名?
我检查了下面的问题,但是我没有得到答案。
How do I print the type or class of a variable in Swift?
How do you find out the type of an object (in Swift)?
import UIKit
class PrintClassName: NSObject {
override init() {
super.init()
println("\(self.dynamicType)")
// Prints "(Metatype)"
println("\(object_getClassName(self))");
// Prints "0x7b680910 (Pointer)"
println("\(self.className)")
// Compile error!
println(PrintClassName.self)
// Prints "(Metatype)"
}
}
2014.08.25 postscript
我检查了问题。
Swift class introspection & generics
但是println(PrintClassName.self)
打印“(元类型)”
我想在不在源代码中写类名的情况下获得类名。
https://stackoverflow.com/questions/25476218
复制相似问题