Collections ยท SWIFT RECIPE

Check whether a value is an array

Inspect an unknown value and access its elements safely.

main.swift
let value: Any = ["Swift", "Foundation"]
print(value is [Any])
if let items = value as? [String] {
    print(items.joined(separator: ", "))
}

How it works

Use is for a type check. Use as? when you also need the typed result. A conditional cast checks the element type and returns nil on failure.

EXPECTED OUTPUT
true
Swift, Foundation

Keep in mind

Cast heterogeneous JSON arrays to [Any], then validate each element.

Read the Swift language reference