What are Swift's counterparts to JavaScript's Array.some() and Array.every()?

后端 未结 2 1121
忘掉有多难
忘掉有多难 2021-01-19 18:33

Swift provides map, filter, reduce, ... for Array\'s, but I am not finding some (or any) or

2条回答
  •  清歌不尽
    2021-01-19 18:57

    Update:

    Use allSatisfy (all) and contains(where:) (some).

    Old answer:

    Just use contains.

    // check if ALL items are completed
    // so it does not contain a single item which is not completed
    !items.contains { !$0.completed }
    
    // check if SOME item is completed
    // so test if there is at least one item which is completed
    items.contains { $0.completed }
    

提交回复
热议问题