相关疑难解决方法(0)

如何在Swift中打印变量的类型或类?

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

types swift

325
推荐指数
16
解决办法
20万
查看次数

如何将Swift类型名称作为带有其名称空间(或框架名称)的字符串

有没有办法将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"作为命名空间.

ios swift

11
推荐指数
1
解决办法
4360
查看次数

标签 统计

swift ×2

ios ×1

types ×1