What Array.contains(_:) Checks
An unexpected result.
Today, I wanted to check whether two types of content were present in an array.
For the sake of example, let’s say we have a Media enum:
enum Media: Equatable {
case text, audio, video
}
Given an array containing .text and .audio, I expected passing both values
to contains(_:) to return true:
let media: [Media] = [.text, .audio]
print(media.contains([.audio, .text])) // false
As you can probably guess from the fact that I’m writing about this, it didn’t.
What contains actually checks
There are two different questions hiding behind that method name. Passing a single element asks whether that value exists in the array:
print(media.contains(.audio)) // true
Passing a collection uses a different overload of contains(_:).
It searches for that collection’s elements together, in the same order.
Both order and adjacency matter:
print(media.contains([.text, .audio])) // true
print(media.contains([.audio, .text])) // false
let separated: [Media] = [.text, .video, .audio]
print(separated.contains([.text, .audio])) // false
In the above snippet, .text comes immediately before .audio, so searching for
[.text, .audio] succeeds. However, searching for [.audio, .text] fails because those
values appear in the opposite order in the array. In separated, the order
matches, but the search still fails because .video divides the two values.
This makes more sense when you think of a substring search:
print("hello".contains("ell")) // true
print("hello".contains("le")) // false
The collection overload was introduced in Swift 5.7 as part of SE-0357, which added string-processing operations and their generic collection equivalents. It’s useful for finding a contiguous sequence; I was looking for membership.
Checking for both values
For my original question, allSatisfy(_:)
expresses the intent directly:
let required: [Media] = [.audio, .text]
let containsBoth = required.allSatisfy { media.contains($0) }
print(containsBoth) // true
For each required value, check whether the array contains it. The values can appear anywhere, in either order.
This checks membership, not how many times a value appears. If required contains
.audio twice, one .audio in media still satisfies the membership check.
For a single call site, I’d use allSatisfy directly. It expresses exactly what I meant to ask: are all of these values present?
If that check comes up repeatedly, a small extension might take this shape.
extension Collection where Element: Equatable {
func containsAll<C: Collection>(_ collection: C) -> Bool where C.Element == Element {
collection.allSatisfy {
contains($0)
}
}
}
print(media.containsAll([.text, .audio])) // true
print(media.containsAll([.audio, .text])) // true