如何在 Swift 中打印变量的类型或类? [英] How do I print the type or class of a variable in Swift?
问题描述
有没有办法在 swift 中打印变量的运行时类型?例如:
Is there a way to print the runtime type of a variable in swift? For example:
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!
.
In the example above, I'm looking for a way to show that the variable "soon" is of type ImplicitlyUnwrappedOptional<NSDate>
, or at least NSDate!
.
推荐答案
2016 年 9 月更新
Swift 3.0:使用 type(of:)
,例如type(of: someThing)
(因为删除了 dynamicType
关键字)
Swift 3.0: Use type(of:)
, e.g. type(of: someThing)
(since the dynamicType
keyword has been removed)
2015 年 10 月更新:
我将下面的示例更新为新的 Swift 2.0 语法(例如,println
被替换为 print
,toString()
现在是 String()
).
I updated the examples below to the new Swift 2.0 syntax (e.g. println
was replaced with print
, toString()
is now String()
).
来自 Xcode 6.3 发行说明:
@nschum 在评论中指出 Xcode6.3 发行说明 展示另一种方式:
@nschum points out in the comments that the Xcode 6.3 release notes show another way:
类型值现在在与println 或字符串插值.
Type values now print as the full demangled type name when used with println or string interpolation.
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>
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
原答案:
在 Xcode 6.3 之前 _stdlib_getTypeName
获得了变量的重整类型名称.Ewan Swick 的博客条目 有助于破译这些字符串:
Prior to Xcode 6.3 _stdlib_getTypeName
got the mangled type name of a variable. Ewan Swick's blog entry helps to decipher these strings:
例如_TtSi
代表 Swift 内部的 Int
类型.
e.g. _TtSi
stands for Swift's internal Int
type.
这篇关于如何在 Swift 中打印变量的类型或类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!