Is there any way to get a Swift type name as a string with its namespace (or framework name)?
有没有办法将Swift类型名称作为带有命名空间(或框架名称)的字符串?
For example, if Foo.framework
has a class named Bar
, I would like to get a string something like "Foo.Bar"
.
例如,如果Foo.framework有一个名为Bar的类,我想得到类似“Foo.Bar”的字符串。
The followings just return the class name "Bar"
.
以下只返回类名“Bar”。
let barName1 = String(Bar.self) // returns "Bar"
let barName2 = "\(Bar.self)" // returns "Bar"
let barName3 = "\(Bar().dynamicType)" // returns "Bar"
I would like to also get the framework name "Foo"
as a namespace.
我还想将框架名称“Foo”作为命名空间。
2 个解决方案
#1
18
Use String(reflecting:)
:
使用String(反映:):
struct Bar { }
let barName = String(reflecting: Bar.self)
print(barName) // <Module>.Bar
From the Xcode 7 Release Notes:
来自Xcode 7发行说明:
Type names and enum cases now print and convert to String without qualification by default.
debugPrint
orString(reflecting:)
can still be used to get fully qualified names.默认情况下,类型名称和枚举案例现在打印并转换为字符串,无需限定。 debugPrint或String(反射:)仍可用于获取完全限定名称。
#2
0
String(reflecting:)
works right now, but could change in the future. So if you only need the fully-qualified name temporarily, you should be fine. However, if the name is something you're going to rely on over a longer period of time (spanning multiple releases of Swift) you shouldn't use it.
字符串(反映:)现在可以使用,但将来可能会改变。因此,如果您只需要临时的完全限定名称,那么您应该没问题。但是,如果名称是您将在更长的时间内(跨越Swift的多个版本)依赖的名称,则不应使用它。
The Swift community is trying to come to a consensus on a longer-term method for getting the information from a type.
Swift社区正试图就从类型中获取信息的长期方法达成共识。
From Joe Groff:
来自Joe Groff:
In 4.2, String(reflecting:) still shows a fully qualified name for local types, but the local type context is anonymized and printed in a way that's intended to be clear is non-stable, since no matter what reflection support we add in the future, it'd be a bad idea to incidentally rely on the presence or identity of local types in dynamic code.
在4.2中,字符串(反射:)仍然显示本地类型的完全限定名称,但本地类型上下文是匿名的,并且打算以明确的方式打印是不稳定的,因为无论我们添加什么反射支持未来,顺便依赖动态代码中本地类型的存在或身份是一个坏主意。
https://forums.swift.org/t/getting-the-more-fully-qualified-name-of-a-type/14375/9
https://forums.swift.org/t/getting-the-more-fully-qualified-name-of-a-type/14375/9
#1
18
Use String(reflecting:)
:
使用String(反映:):
struct Bar { }
let barName = String(reflecting: Bar.self)
print(barName) // <Module>.Bar
From the Xcode 7 Release Notes:
来自Xcode 7发行说明:
Type names and enum cases now print and convert to String without qualification by default.
debugPrint
orString(reflecting:)
can still be used to get fully qualified names.默认情况下,类型名称和枚举案例现在打印并转换为字符串,无需限定。 debugPrint或String(反射:)仍可用于获取完全限定名称。
#2
0
String(reflecting:)
works right now, but could change in the future. So if you only need the fully-qualified name temporarily, you should be fine. However, if the name is something you're going to rely on over a longer period of time (spanning multiple releases of Swift) you shouldn't use it.
字符串(反映:)现在可以使用,但将来可能会改变。因此,如果您只需要临时的完全限定名称,那么您应该没问题。但是,如果名称是您将在更长的时间内(跨越Swift的多个版本)依赖的名称,则不应使用它。
The Swift community is trying to come to a consensus on a longer-term method for getting the information from a type.
Swift社区正试图就从类型中获取信息的长期方法达成共识。
From Joe Groff:
来自Joe Groff:
In 4.2, String(reflecting:) still shows a fully qualified name for local types, but the local type context is anonymized and printed in a way that's intended to be clear is non-stable, since no matter what reflection support we add in the future, it'd be a bad idea to incidentally rely on the presence or identity of local types in dynamic code.
在4.2中,字符串(反射:)仍然显示本地类型的完全限定名称,但本地类型上下文是匿名的,并且打算以明确的方式打印是不稳定的,因为无论我们添加什么反射支持未来,顺便依赖动态代码中本地类型的存在或身份是一个坏主意。
https://forums.swift.org/t/getting-the-more-fully-qualified-name-of-a-type/14375/9
https://forums.swift.org/t/getting-the-more-fully-qualified-name-of-a-type/14375/9