Swift .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.

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

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

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours