cfi*_*her 8 cocoa objective-c lldb swift
在Objective-C您NSLog对象或po其中时lldb,对象接收description消息.
然而,在Swift中,行为似乎有所不同.我实现了两个Printable(需要description属性)和DebugPrintable(它需要一个名为的属性debugDescription).如果我尝试println()对象或po它,则不会调用这些属性.
这是怎么回事?那些协议是什么?
有一个已知的问题Printable被Swift REPL忽略(即,Playground中的任何内容或xcrun swift在命令行中运行),但由编译器识别(在模拟器中编译的应用程序或xcrun swiftc).
例如,这是"foo.swift"的代码:
struct Person : Printable {
let name: String
var description: String {
return "\(name)"
}
}
let me = Person(name: "Nate")
println(me)
Run Code Online (Sandbox Code Playgroud)
如果我使用REPL运行它,我得到这个:
$ xcrun swift foo.swift
foo.Person
Run Code Online (Sandbox Code Playgroud)
但是,如果我先编译它,然后运行它,它使用description计算属性:
$ xcrun -sdk macosx swiftc foo.swift ; ./foo
Nate
Run Code Online (Sandbox Code Playgroud)
的DebugPrintable,如果你希望能够使用的协议是非常有用的debugPrint,并debugPrintln在编译的代码,他们打印出一个实例的-功能debugDescription属性.