Library reference

Find an item in a list

Check whether a particular value appears anywhere in a list. A single match is enough.

Array.contains

Simple example

Does the list contain two?

Start1 · 2 · 3
Resulttrue
main.swift
let values = [1, 2, 3]
print(values.contains(2))
Output
true
Advertisements

Everyday examples

Is ticket 17 on the pickup list?

Start15 · 17 · 20
Resulttrue
main.swift
let values = [15, 17, 20]
print(values.contains(17))
Output
true

Does this bus stop at stop 6?

Start2 · 4 · 8
Resultfalse
main.swift
let values = [2, 4, 8]
print(values.contains(6))
Output
false

Is parcel 42 on the van?

Start42 · 18 · 15
Resulttrue
main.swift
let values = [42, 18, 15]
print(values.contains(42))
Output
true
Run the example
Terminal
swift main.swift
Official documentation