Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

swift

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. po myStruct results in

▿ 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

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

I thought it might have to do with CustomDebugStringConvertible, so I added this code:

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?

like image 294
Tim Vermeulen Avatar asked Jan 20 '16 18:01

Tim Vermeulen


People also ask

How do you print the address of a variable in Swift?

To get the (stack) address of a struct, build-in type or object reference. In Objective-C this would be [NSString stringWithFormat:@"%p", &o] or [NSString stringWithFormat:@"%p", &i] . s is struct. So if s is assigned to another variable s2 , the value will be copied and the returned address for s2 will be different.

How do I print a SwiftUI view?

Use print statement inside SwiftUI view You need to assign print() to a variable ( var ) or a constant ( let ). Here is an example of how we can use print() in a SwiftUI view. Text("Hello, World!") 1 and 2 We don't use the returning result ( x and y ) anywhere.

What is debugging in Swift?

Integrated debugging. The most obvious benefit is that the Swift REPL is also a full-featured debugger. It's trivial to declare a function, set a breakpoint in it, and then call it. When execution stops at a breakpoint the full feature set of the debugger is immediately available.


1 Answers

(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.
like image 142
user3441734 Avatar answered Nov 17 '22 21:11

user3441734