有没有办法在swift中打印变量的运行时类型?例如:
var now = NSDate()
var soon = now.dateByAddingTimeInterval(5.0)
println("\(now.dynamicType)")
// Prints "(Metatype)"
println("\(now.dynamicType.description()")
// Prints "__NSDate" since objective-c Class objects have a "description" selector
println("\(soon.dynamicType.description()")
// Compile-time error since ImplicitlyUnwrappedOptional<NSDate> has no "description" method
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我正在寻找一种方法来显示变量"很快"是类型ImplicitlyUnwrappedOptional<NSDate>,或至少NSDate!.
有没有办法将Swift类型名称作为带有命名空间(或框架名称)的字符串?
例如,如果Foo.framework有一个名为的类Bar,我想得到类似的字符串"Foo.Bar".
以下只返回类名"Bar".
let barName1 = String(Bar.self) // returns "Bar"
let barName2 = "\(Bar.self)" // returns "Bar"
let barName3 = "\(Bar().dynamicType)" // returns "Bar"
Run Code Online (Sandbox Code Playgroud)
我还想将框架名称"Foo"作为命名空间.