How to get Swift type name as a string with its namespace (or frame name)

Is there a way to get a Swift type name as a string with its namespace (or frame name)?

For example, if Foo.framework has a class called Bar , I would like to get a string like "Foo.Bar" .

After that, just return the class name "Bar" .

 let barName1 = String(Bar.self) // returns "Bar" let barName2 = "\(Bar.self)" // returns "Bar" let barName3 = "\(Bar().dynamicType)" // returns "Bar" 

I would also like to get the frame name "Foo" as a namespace.

+7
ios swift
source share
1 answer

Use String(reflecting:) :

 struct Bar { } let barName = String(reflecting: Bar.self) print(barName) // <Module>.Bar 

From the Xcode 7 Release Notes :

Type names and enumerations are now printed and converted to String with no default. debugPrint or String(reflecting:) can still get full names.

+16
source share

All Articles