Arrays
-
Fix: Getting the First N Elements of an Array in Swift
Get the first N elements of an array in Swift with prefix, convert the result to an Array, and avoid out-of-range errors…
-

Remove an Element From a Swift Array (and Fix the ‘let’ Error)
Remove items from a Swift array by index or value, fix the ‘Cannot use mutating member on immutable value’ error, and pick…
-

How to Sort an Array of Custom Objects by Property in Swift
Learn why an untyped array blocks sorting and how to sort custom Swift objects by a property ascending or descending with sorted(by:)…
-

How to Add an Element to a Swift Array (Append, Insert, Prepend)
Swift has no push or unshift: use append, insert(at:) and append(contentsOf:) correctly, and learn why prepending an element is never constant time.
-

Swift Array Contains: Check if an Element Exists
Swift arrays have no `include` or `has`. Use `contains(_:)` for Equatable values, `contains(where:)` for predicates, and a Set for fast repeated lookups.
-

Swift enumerated(): Loop With Index and Element Together
Use enumerated() to loop over a Swift array with both index and element, build arrays of pairs, and avoid the offset-vs-index trap…
-

How to Find the Index of an Item in a Swift Array
Find an item’s index in a Swift array with firstIndex(of:), lastIndex, and identity checks for classes. Fix the old find/indexOf errors and…