Member-only story
13 Useful Array Methods in Swift
Use Swift’s built-in array methods to make your life easier and code prettier
An array is a commonly used data type in Swift. It is like a list in which you store items (for example numbers). You are going to use arrays a lot as an iOS engineer.
To help you master arrays and improve your code, I wrote this guide that shows you built-in array methods and examples.
1. Array.filter()
Instead of creating a loop, you can use the array’s built-in filter()
method to filter elements to a new array.
For instance, let’s filter numbers lower than or equal to 3
to a new array:
Output:
[1,2,3]
2. Array.map()
The map()
method takes each value of an array, performs some action on it, and finally places the new value into a new array. This is handy and can be used instead of a for
or while
loop.