swift - Add line break to Xcode output -
say have custom object has custom description so:
class customobject { var customdescription: string { return "title: hello, \n subtitle: world" } } is there way have line break \n work in console when printing using po command in lldb console?
right lldb prints \n part of text , doesn't process it:
po object.customdescription > "title: hello, \n subtitle: world" where desired result is:
po object.customdescription > title: hello subtitle: world do have solution this?
i don't want discourage making lldb data formatter classes. have advantage on po give lldb way show summary representation of data without having call code in running program. also, if use xcode, xcode locals view show data formatters automatically.
but clear way po works:
the lldb po command works calling language-specified description method object expression provide resolves to. instance in example, seeing result of description method swift string class, displays "programmers view" of string.
in case of swift, there couple of ways provide description. simplest implement customstringconvertible protocol:
class customobject : customstringconvertible { var description: string { return "title: hello,\n subtitle: world" } } func main() { let my_object = customobject() print (my_object) } main() then when debug this, see:
(lldb) br s -p print breakpoint 1: = desc`desc.main() -> () + 27 @ desc.swift:11, address = 0x000000010000116b (lldb) run process 69112 launched: '/private/tmp/desc' (x86_64) process 69112 stopped * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 frame #0: 0x000000010000116b desc`desc.main() -> () @ desc.swift:11 8 func main() 9 { 10 let my_object = customobject() -> 11 print (my_object) ^ 12 } 13 14 main() target 0: (desc) stopped. (lldb) po my_object title: hello, subtitle: world (lldb) c process 69112 resuming title: hello, subtitle: world process 69112 exited status = 0 (0x00000000) note here both po , swift print function render object same way.
if want separate debug , print descriptions, implement customdebugstringconvertible requires debugdescription property. po print debugdescription print print description.
if want fancier, or want have po represent "structure" of object way po result swift arrays (for example) does, can make custom mirror class. there's documentation on here:
https://developer.apple.com/documentation/swift/mirror
and if isn't clear, implementation of mirrors in swift sources in:
https://github.com/apple/swift/blob/master/stdlib/public/core/mirror.swift
Comments
Post a Comment