9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift4]配列の中の全ての要素が条件を満たすか判定する

Last updated at Posted at 2019-03-04

Swift4.2から使えます。

allSatisfy

シーケンスのすべての要素が特定の述語を満たすかどうかを示すブール値を返します。
func allSatisfy(_ predicate: (Element) throws -> Bool) rethrows -> Bool


// 全ての値が偶数が判定する
let values = [2, 4, 6, 8, 10]
let isAllEvenNumber = values.allSatisfy { $0 % 2 == 0 } // isAllEvenNumber == true

// 全ての値が5文字以上か判定する
let names = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"]
let allHaveAtLeastFive = names.allSatisfy({ $0.count >= 5 }) // allHaveAtLeastFive == true
9
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?