.allSatisfy()

dells1234's avatar
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.

Syntax

array.allSatisfy(condition)

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

Example

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})
print(evenNumbers)

This example results in the following output:

true

All contributors

Contribute to Docs

Learn Swift on Codecademy