簡體   English   中英

如何在調試時打印Swift結構的自定義說明,而沒有其他內容?

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

說我有一個這樣的結構:

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

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

從代碼中打印描述可以正常工作。

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

這導致

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

當我想從調試器打印myStruct的描述時出現問題。 po myStruct結果在

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

顯式打印其描述也無濟於事,因為po myStruct.description導致

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

我認為這可能與CustomDebugStringConvertible ,所以我添加了以下代碼:

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

不幸的是,這根本不會改變任何結果。

有沒有辦法

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

在調試時從命令行打印?

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

您可以定義自己的“命令”

(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