How to show all the elements of an array in swift?

You can simply iterate through the array like this and print out all elements on a new line:

for element in array {
  println(element)
}

UPDATE

For Swift 2 and Swift 3:

for element in array {
  print(element)
}

Or if you want it on the same line:

for element in array {
  print(element, terminator: " ")
}

Update:

Starting in iOS 9 you can now just use dump

var someArray = ["one", "two", "three"]
dump(someArray)

Original:

This is a nice way to print arrays:

var someArray = ["one", "two", "three"]

// prints out the elements separated by a line break
// same as calling "println" on each item in the array:
println(someArray.joinWithSeparator("\n"))

// one
// two
// three

Otherwise if you want them on the same line you can just simply print the array:

// prints on the same line:
// ["one", "two", "three"]
println(someArray)

My personal favorite for debugging purposes is dump() which also prints which index the element has. Perfect if you have arrays within an array too.

var array = ["Chinese", "Italian", "Japanese", "French", "American"]
dump(array)

This will generate the following output

▿ 5 elements
  - [0]: Chinese
  - [1]: Italian
  - [2]: Japanese
  - [3]: French
  - [4]: American

Tags:

Ios

Xcode

Swift