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

Use String(reflecting:):

struct Bar { }

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

From the Xcode 7 Release Notes:

Type names and enum cases now print and convert to String without
qualification by default. debugPrint or String(reflecting:) can still
be used to get fully qualified names.

Leave a Comment