qwe*_*_so 1 debug-symbols println swift
如果您在Playgroud中运行以下内容
class Test {
var description:String {
return "This is Test"
}
}
class Test1:NSObject {
override var description:String {
return "This is Test"
}
}
let t = Test()
println(t)
let t1 = Test1()
println(t1)
Run Code Online (Sandbox Code Playgroud)
你看到第一个println会输出一些调试器模糊,而第二个会输出内容description.
所以:是有办法,"正常"类将被视为相同的方式的子类NSObject,以便println将尊重的内容description属性?
Mar*_*n R 10
从println()API文档:
/// Writes the textual representation of `object` and a newline character into
/// the standard output.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type. Instead, adopt one of the
/// protocols mentioned above.
func println<T>(object: T)
Run Code Online (Sandbox Code Playgroud)
因此,为了获得自定义println()表示,您的类必须(例如)Printable明确采用协议:
class Test : Printable {
var description:String {
return "This is Test"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这在Xcode 6.1.1的Playground中不起作用.它已在Xcode 6.3 beta中修复.从发行说明:
•在Playground中添加一致性现在可以按预期工作,...
我在API头文件中找不到这个,但似乎NSObject
已知(及其子类)符合Printable.因此,自定义说明适用于您的Test1班级.
在夫特2(Xcode中7),该Printable协议已经被重新命名为CustomStringConvertible:
class Test : CustomStringConvertible {
public var description:String {
return "This is Test"
}
}
let t = Test()
print(t)
// This is Test
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4903 次 |
| 最近记录: |