Swift中的Printable和DebugPrintable之间的区别 [英] Difference between Printable and DebugPrintable in Swift
本文介绍了Swift中的Printable和DebugPrintable之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
找到Swift等效的Cocoa description
,我在Swift中找到了以下协议: Printable
和 DebugPrintable
。
这两个协议有什么区别,我应该什么时候使用?
$ b $在XCode 6 Beta版本(版本6.2(6C101)),我发现println和debugPrintln使用描述if-and-only - 如果类从NSObject下降。我看不到或者使用debugDescription,但是在一个Playground中运行debugPrintln只输出到控制台,并不出现在操场本身。 import Foundation
class Tdesc:NSObject,Printable,DebugPrintable {
override var description:String {returnA description}
override var debugDescription: String {returnA debugDescription}
}
类Xdesc:Printable,DebugPrintable {
var description:String {returnA description}
var debugDescription: String {returnA debugDescription}
}
let t = Tdesc()
let x = Xdesc()
t.description
let z:String =x \(t)
println(t)//在游乐场和控制台中显示描述
debugPrintln(t)//在游乐场中不显示任何内容,但在控制台中的描述
x.description
let y:String =x\ )
println(x)//在游乐场和控制台中显示__lldb_expr_405.Xdesc
debugPrintln(x)
Looking for a Swift equivalent of Cocoa's description
, I found the following protocols in Swift: Printable
and DebugPrintable
.
What's the difference between these two protocols and when should I use each one?
解决方案
In XCode 6 Beta (Version 6.2 (6C101)) I find that both println and debugPrintln use description if-and-only-if the class descends from NSObject. I don't see that either uses debugDescription at all but when run in a Playground debugPrintln outputs only to the Console and doesn't appear in the playground itself.
import Foundation
class Tdesc: NSObject, Printable, DebugPrintable {
override var description: String {return "A description"}
override var debugDescription: String {return "A debugDescription"}
}
class Xdesc: Printable, DebugPrintable {
var description: String {return "A description"}
var debugDescription: String {return "A debugDescription"}
}
let t = Tdesc()
let x = Xdesc()
t.description
let z: String = "x\(t)"
println(t) // Displays "A description" in the Playground and Console
debugPrintln(t) // Displays nothing in the Playground but "A description" in the Console
x.description
let y: String = "x\(x)"
println(x) // Displays "__lldb_expr_405.Xdesc" in the Playground and Console
debugPrintln(x)
这篇关于Swift中的Printable和DebugPrintable之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文