38

Environment: Swift, Xcode 6

How do I get a list (po) of data items of a Swift array?

The following is Swift code for building a simple array:

kindArray += "Two"
kindArray.append("Two")

var myStringArray: String[]
myStringArray = ["One", "Two"]
myStringArray.append("Three")
myStringArray += "Four"

var firstItem = myStringArray[0]

Here's the debug output:

(lldb) po firstItem
"One"

(lldb) po kindArray
Some
 {
  Some = 0x0ffb0000 {}
}
(lldb) po myStringArray
size=1
 {
  [0] = {
    core = {
      _baseAddress = Builtin.RawPointer = 0x00000008
      _countAndFlags = 34718
      _owner = Some {
        Some = (instance_type = Builtin.RawPointer = 0x80000003)
      }
    }
  }
}

All I'm getting is 'Some' and 'size'.
I would like to show the contents.

Ilya Luzyanin
  • 7,910
  • 4
  • 29
  • 49
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • 1
    I think `expr` is supposed to be better for dumping Swift types to the debugger console. (But I'm posting from my iPhone, where there's no LLDB to play with.) Also note you can type `repl` in the debugger to get an interactive Swift environment, much like a playground, with the current state of your debugging session. – rickster Jun 15 '14 at 20:54
  • I think they may not just not yet have implemented the kind of mapping they need to do for array and dictionary debugging in Xcode. – Nate Cook Jun 16 '14 at 15:10
  • In both cases (repo & expr) I get the same 'po' result. So I suspect this feature isn't fully implemented, – Frederick C. Lee Jun 16 '14 at 22:59

3 Answers3

67

Just do:

po print(myStringArray)
benka
  • 4,732
  • 35
  • 47
  • 58
acastano
  • 807
  • 9
  • 9
13

You should be able to take advantage of the Printable or DebugPrintable protocols. Simply print out the description or debugDescription property:

po myStringArray.description
po myStringArray.debugDescription
drewag
  • 93,393
  • 28
  • 139
  • 128
  • Doesn't work for me: myStringArray: [(Square, 25), (Square, 16),...] ------> (lldb) po myStringArray.description error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x4d03ee60). The process has been returned to the state before expression evaluation --------> (lldb) po myStringArray size=1 { [0] = (0 = Swift.String @ 0x7fbd0e3d6f70, 1 = 0) } ------> (lldb) po myStringArray.debugDescription error: :1:1: error: 'Optional>' does not have a member named 'debugDescription' myStringArray.debugDescription ^ ~~~~~~~~~~~~~~~~ – Frederick C. Lee Jun 17 '14 at 01:32
  • Are you running the code in your example above? It looks like you are storing something else in the array. I ran your code and my `po` statements work great. – drewag Jun 17 '14 at 01:34
  • I was able to do a println(); but not a po via debugger. Perhaps the debugger isn't correctly set? ...I'm using the standard debugger defaults. – Frederick C. Lee Jun 17 '14 at 02:45
  • I tried to access the first element of the array: let RicTuple = myStringArray[0]; but got the following error: "'(String, Int)[]?' does not have a member named 'subscript'" – Frederick C. Lee Jun 17 '14 at 02:55
  • @FrederickC.Lee I think you are getting bad data into your array somehow. It is also potentially a bug in the compiler, but I am able to print out arrays of strings just fine (including with the code in your question) – drewag Jun 17 '14 at 04:53
3
po myStringArray.map{ $0 }

if you have an array of custom objects

po myArray.map.{ $0.customProperty }
dlp
  • 581
  • 5
  • 14