How to print a custom description of the Swift structure during debugging, and nothing more?

Say I have a structure like this:

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

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

Printing the description from the code is working fine.

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

The result is

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

Problems arise when I want to print a description myStructfrom the debugger. po myStructleads to

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

Explicitly printing out its description also does not help, because it po myStruct.descriptionleads to

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

I thought this could be due to CustomDebugStringConvertible, so I added this code:

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

Unfortunately, this in no way alters any results.

Is there any way to have

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

printed from the command line during debugging?

+4
source share
1 answer
(lldb) expression print(myStruct)
my int is 3,
and my string is "hello"

you can define your own team

(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.
+1

Source: https://habr.com/ru/post/1625179/


All Articles