I have an array like this: var array = ["Chinese", "Italian", "Japanese", "French", "American"]
I want to print out all separate elements on a new line.
How can I do this?
To filter an array in Swift: Call the Array. filter() method on an array. Pass a filtering function as an argument to the method.
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
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: " ") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With