How to check whether every element of an array satisfies a given condition in Swift

3 Answers

0 votes
let arr = [2, 4, 6, 8, 10, 12]

let allEven = arr.allSatisfy({$0 % 2 == 0})

print(allEven)





/*
run:

true

*/

 



answered Jun 13, 2023 by avibootz
0 votes
let arr = ["swift", "java", "python", "rust"]

let allBiggerLetters = arr.allSatisfy({$0.count > 3})

print(allBiggerLetters)






/*
run:

true

*/

 



answered Jun 13, 2023 by avibootz
0 votes
let arr = ["swift", "swift", "swift", "swift"]

let allAreSwift = arr.allSatisfy({$0 == "swift"})

print(allAreSwift)






/*
run:

true

*/

 



answered Jun 13, 2023 by avibootz
...