Frederick C. Lee
Frederick C. Lee

Reputation: 9513

How do I list (po) the contents of a Swift Array?

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.

Upvotes: 38

Views: 22026

Answers (3)

dlp
dlp

Reputation: 581

po myStringArray.map{ $0 }

if you have an array of custom objects

po myArray.map.{ $0.customProperty }

Upvotes: 3

acastano
acastano

Reputation: 807

Just do:

po print(myStringArray)

Upvotes: 67

drewag
drewag

Reputation: 94753

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

Upvotes: 13

Related Questions