Published Jun 26, 2023
Contribute to Docs

.allSatisfy() is a Swift method that checks if all the elements in an array satisfy a given condition.



condition: An expression that is used to test the values in the array.


In the example below, an array called numbers is declared. Then the .allSatisfy() method is used to evaluate if the elements of numbers are all even (divisible by two).

// Create an array
let numbers = [2, 4, 6, 8]
// Specify the condition to be tested
let evenNumbers = numbers.allSatisfy({$0 % 2 == 0})

This example results in the following output:


All contributors

Looking to contribute?

Learn Swift on Codecademy