The Fundamentals
In the realm of Swift, the array filter() method is used for returning a new array that contains only the elements of the original array that satisfy a given condition. The syntax of the filter() method is shown below:
array.filter(condition)
Here, array is an object of the Array class, and condition is a closure that accepts an element of the array as its parameter and returns a Boolean value indicating whether the element should be included in the new array.
In general, you will call the filter() method like so:
array.filter { (element) -> Bool in
// code to test element and return true or false
}
element is a placeholder for each element of the array, and the code inside the curly braces is the logic to test the element and return true or false.
Now, it’s time for practice. Let’s see some practical examples and write some code to strengthen your understanding.
Examples
These examples are arranged in order from basic to advanced, from simple to complex.
Example 1: Filtering out negative numbers from an array of integers
In this example, we use the filter() method to filter out negative numbers from a numbers array. We use a short-hand closure that takes each element as $0 and returns true if $0 is greater than or equal to zero and false otherwise.
// an array of integers
let numbers = [1, -2, 3, -4, 5]
// use filter() to return only non-negative numbers
let filtered = numbers.filter { $0 >= 0 }
print(filtered)
Output:
[1, 3, 5]
Example 2: Filtering out empty strings from an array of strings
n this example, we use the filter() method to filter out empty strings from an array of strings. We use a short-hand closure that takes each element as $0 and returns true if $0 is not empty and false otherwise. The ! operator negates the Boolean value. A new array will be returned and it will only contain the non-empty strings.
let strings = ["hello", "", "world", "", "swift", "Sling Academy"]
let filtered = strings.filter { !$0.isEmpty }
print(filtered)
Output:
["hello", "world", "swift", "Sling Academy"]
Example 3: Filtering out odd numbers from an array using a custom function
What we are going to do is to filter out odd numbers from an array of numbers using the filter() method and a custom function. The custom function will be passed as the condition to the filter() method.
// an array of integers
let numbers = [1, 2, 3, 4, 5]
// define a custom function that takes an integer and returns true if it is even, false otherwise
func isEven(_ number: Int) -> Bool {
return number % 2 == 0
}
// use filter() and pass the custom function as the condition
let filtered = numbers.filter(isEven)
print(filtered)
Output:
[2, 4]
Example 4: Filtering out users below a certain age from an array of custom objects
This example uses the filter() method to filter out users below a certain age from an array of custom objects. We will define a struct called User that has two properties: name and age and generate an array of User objects with different names and ages. We use a short-hand closure that takes each user as $0 and returns true if $0.age is greater than or equal to 25, and false otherwise.
// define User struct
struct User {
let name: String
let age: Int
}
// an array of custom objects
let users = [
User(name: "Little Batman", age: 20),
User(name: "Old Batman", age: 125),
User(name: "Corupttd Monk", age: 930),
]
// use filter() to return only users with age greater than or equal to 25
let filtered = users.filter { $0.age >= 25 }
// print the result
print(filtered)
Output:
[
main.User(name: "Old Batman", age: 125),
main.User(name: "Corupttd Monk", age: 930)
]