有没有办法在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

在上面的例子中,我正在寻找一种方法来显示变量"soon"的类型是ImplicitlyUnwrappedOptional<NSDate>,或者至少是NSDate!.

推荐答案

Update September 2016

Swift 3.0:使用type(of:),例如type(of: someThing)(因为dynamicType关键字已被删除)

Update October 2015:

我将下面的示例更新为新的Swift 2.0语法(例如,printlnprint取代,toString()现在是String()).

From the Xcode 6.3 release notes:

@nschum在 comments 中指出,Xcode 6.3 release notes个代表着另一种方式:

当与一起使用时,类型值现在打印为完整的demangled类型名称

import Foundation

class PureSwiftClass { }

var myvar0 = NSString() // Objective-C class
var myvar1 = PureSwiftClass()
var myvar2 = 42
var myvar3 = "Hans"

print( "String(myvar0.dynamicType) -> \(myvar0.dynamicType)")
print( "String(myvar1.dynamicType) -> \(myvar1.dynamicType)")
print( "String(myvar2.dynamicType) -> \(myvar2.dynamicType)")
print( "String(myvar3.dynamicType) -> \(myvar3.dynamicType)")

print( "String(Int.self)           -> \(Int.self)")
print( "String((Int?).self         -> \((Int?).self)")
print( "String(NSString.self)      -> \(NSString.self)")
print( "String(Array<String>.self) -> \(Array<String>.self)")

哪些输出:

String(myvar0.dynamicType) -> __NSCFConstantString
String(myvar1.dynamicType) -> PureSwiftClass
String(myvar2.dynamicType) -> Int
String(myvar3.dynamicType) -> String
String(Int.self)           -> Int
String((Int?).self         -> Optional<Int>
String(NSString.self)      -> NSString
String(Array<String>.self) -> Array<String>

Update for Xcode 6.3:

您可以使用_stdlib_getDemangledTypeName():

print( "TypeName0 = \(_stdlib_getDemangledTypeName(myvar0))")
print( "TypeName1 = \(_stdlib_getDemangledTypeName(myvar1))")
print( "TypeName2 = \(_stdlib_getDemangledTypeName(myvar2))")
print( "TypeName3 = \(_stdlib_getDemangledTypeName(myvar3))")

并将其作为输出:

TypeName0 = NSString
TypeName1 = __lldb_expr_26.PureSwiftClass
TypeName2 = Swift.Int
TypeName3 = Swift.String

Original answer:

在Xcode 6.3之前,_stdlib_getTypeName获得了一个变量的损坏类型名称.Ewan Swick's blog entry有助于破译这些字符串:

e、 g._TtSi代表Swift的内部Int型.

Mike Ash has a great blog entry covering the same topic

Swift相关问答推荐

SwiftUI轨迹绘制怪异

它是RxSwift中直接用于可扩展视图的有效的可扩展BehaviorRelay值?

带DispatchSourceTimer的定时器

如何根据 DatePicker 中选定的日期实现 TabView 页面切换?

SwiftUI如何能够在Text中使用字符串字面量来创建LocalizedStringKey?

为什么 SwiftUI 中 ForEach 的这个 Select 不起作用?

为什么在Swift中每次调用Decimal类型的formatted()方法都会越来越慢?

try 制作一个 Swift 扩展来替换字符串中的多次出现

将基于MyProtocol的泛型函数的参数更改为使用存在的any MyProtocol或some MyProtocol是否会受到惩罚?

使 Picker 与其他 BinaryInteger 类型兼容

在 Swift 中实现自定义异步序列

Swift中的分段错误

在 Swift 中获取双精度的小数部分

Swift 中的 PageViewController 当前页面索引

CMutablePointer - 如何访问它?

Swift 中惰性 var 的优势是什么

Swift:如何从函数返回类类型

3D touch /强制touch 实现

swift - 我可以从我的自定义初始化方法中调用 struct 默认成员初始化吗?

swift上的UIImage无法判断nil