简体   繁体   English

如何在调试时打印Swift结构的自定义说明,而没有其他内容?

[英]How do I print a custom description of a Swift struct while debugging, and nothing else?

Say I have a struct like this one: 说我有一个这样的结构:

struct MyStruct: CustomStringConvertible {
    let myInt: Int
    let myString: String

    var description: String {
        return "my int is \(myInt),\nand my string is \"\(myString)\""
    }
}

Printing a description from the code works fine. 从代码中打印描述可以正常工作。

let myStruct = MyStruct(myInt: 3, myString: "hello")
print(myStruct)

This results in 这导致

my int is 3,
and my string is "hello"

Problems arise when I want to print myStruct 's description from the debugger. 当我想从调试器打印myStruct的描述时出现问题。 po myStruct results in po myStruct结果在

▿ my int is 3,
and my string is "hello"
  - myInt : 3
  - myString : "hello"

Explicitly printing out its description doesn't help either, as po myStruct.description results in 显式打印其描述也无济于事,因为po myStruct.description导致

"my int is 3,\nand my string is \"hello\""

I thought it might have to do with CustomDebugStringConvertible , so I added this code: 我认为这可能与CustomDebugStringConvertible ,所以我添加了以下代码:

extension MyStruct: CustomDebugStringConvertible {
    var debugDescription: String {
        return description
    }
}

Unfortunately, this doesn't change any of the outcomes at all. 不幸的是,这根本不会改变任何结果。

Is there a way to just have 有没有办法

my int is 3,
and my string is "hello"

printed from the command line while debugging? 在调试时从命令行打印?

(lldb) expression print(myStruct)
my int is 3,
and my string is "hello"

you can defined your own 'command' 您可以定义自己的“命令”

(lldb) help command
The following subcommands are supported:

      alias   -- Allow users to define their own debugger command
                 abbreviations.  This command takes 'raw' input (no need to
                 quote stuff).
      delete  -- Allow the user to delete user-defined regular expression,
                 python or multi-word commands.
      history -- Dump the history of commands in this session.
      regex   -- Allow the user to create a regular expression command.
      script  -- A set of commands for managing or customizing script commands.
      source  -- Read in debugger commands from the file <filename> and execute
                 them.
      unalias -- Allow the user to remove/delete a user-defined command
                 abbreviation.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM