Array stores ordered, zero-indexed collections with value semantics—mutating a copied array does not affect the original when elements are value types.
Creating and mutating
var nums = [1, 2, 3]
nums.append(4)
nums.insert(0, at: 0)
let doubled = nums.map { $0 * 2 }
Common operations
count,isEmpty,first,lastfilter,map,reduce,sorted()- Subscript:
nums[0]—bounds-checked at runtime
Important interview questions and answers
- Q: Array vs NSArray?
A: SwiftArraybridges to Foundation; prefer typed Swift arrays in new code. - Q: Copy-on-write?
A: Arrays share storage until mutated—efficient passing without eager deep copies.
Self-check
- How do you append to a var array?
- What happens if you subscript out of bounds?
Tip: Arrays use copy-on-write—efficient until one copy mutates shared storage.
Interview prep
- Copy-on-write?
Arrays share storage until mutated—efficient passing.
- Out of bounds?
Runtime trap on invalid subscript.