获取对象的类名为String,使用:
object_getClassName(myViewController)
返回如下内容:
_TtC5AppName22CalendarViewController
我正在寻找纯粹的版本:“CalendarViewController”。我如何得到一个清理类名字符串代替?
我发现了一些关于这个问题的尝试,但没有一个实际的答案。难道根本不可能吗?
获取对象的类名为String,使用:
object_getClassName(myViewController)
返回如下内容:
_TtC5AppName22CalendarViewController
我正在寻找纯粹的版本:“CalendarViewController”。我如何得到一个清理类名字符串代替?
我发现了一些关于这个问题的尝试,但没有一个实际的答案。难道根本不可能吗?
当前回答
如果你不喜欢这个乱七八糟的名字,你可以口述你自己的名字:
@objc(CalendarViewController) class CalendarViewController : UIViewController {
// ...
}
然而,从长远来看,最好是学会解析这个混乱的名称。格式是标准的、有意义的,不会改变。
其他回答
我建议这样的方法(非常Swifty):
// Swift 3
func typeName(_ some: Any) -> String {
return (some is Any.Type) ? "\(some)" : "\(type(of: some))"
}
// Swift 2
func typeName(some: Any) -> String {
return (some is Any.Type) ? "\(some)" : "\(some.dynamicType)"
}
它既不使用内省,也不使用手动提取(没有魔法!)
下面是一个演示:
// Swift 3
import class Foundation.NSObject
func typeName(_ some: Any) -> String {
return (some is Any.Type) ? "\(some)" : "\(type(of: some))"
}
class GenericClass<T> {
var x: T? = nil
}
protocol Proto1 {
func f(x: Int) -> Int
}
@objc(ObjCClass1)
class Class1: NSObject, Proto1 {
func f(x: Int) -> Int {
return x
}
}
struct Struct1 {
var x: Int
}
enum Enum1 {
case X
}
print(typeName(GenericClass<Int>.self)) // GenericClass<Int>
print(typeName(GenericClass<Int>())) // GenericClass<Int>
print(typeName(Proto1.self)) // Proto1
print(typeName(Class1.self)) // Class1
print(typeName(Class1())) // Class1
print(typeName(Class1().f)) // (Int) -> Int
print(typeName(Struct1.self)) // Struct1
print(typeName(Struct1(x: 1))) // Struct1
print(typeName(Enum1.self)) // Enum1
print(typeName(Enum1.X)) // Enum1
你可以像这样使用Swift标准库函数_stdlib_getDemangledTypeName:
let name = _stdlib_getDemangledTypeName(myViewController)
斯威夫特3.0
字符串(描述:MyViewController.self)
如果你的类型是Foo,下面的代码会在Swift 3和Swift 4中给你“Foo”:
let className = String(describing: Foo.self) // Gives you "Foo"
这里大多数答案的问题是,他们给你“Foo”。当你没有该类型的任何实例时,当你真正想要的只是"Foo"时,输入"作为结果字符串。下面是“Foo”。输入“”,就像在其他答案中提到的那样。
let className = String(describing: type(of: Foo.self)) // Gives you "Foo.Type"
如果您只想要“Foo”,则不需要使用类型(of:)部分。
我在Swift 2.2中使用它
guard let currentController = UIApplication.topViewController() else { return }
currentController.classForCoder.description().componentsSeparatedByString(".").last!