.allSatisfy()
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 arraylet numbers = [2, 4, 6, 8]// Specify the condition to be testedlet evenNumbers = numbers.allSatisfy({$0 % 2 == 0})print(evenNumbers)
This example results in the following output:
true
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.