Swift Replace String array at index
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Replacing an element at a specific index in a Swift array is done with subscript assignment: array[index] = newValue. Swift arrays are value types and are zero-indexed, so array[0] is the first element. You can also replace ranges of elements, use replaceSubrange, or swap elements. This article covers all the common array modification patterns in Swift with practical examples.
Basic Replacement by Index
The array must be declared with var (not let) to allow modification.
Safe Index Access
Array Extension for Safe Subscript
Replacing a Range
Range replacement uses ArraySlice assignment. The replacement array can be shorter or longer than the range.
Using replaceSubrange
Swapping Elements
Conditional Replacement
Finding and Replacing
Common Pitfalls
- Accessing an out-of-bounds index:
array[index] = valuecrashes with "Fatal error: Index out of range" ifindex >= array.countorindex < 0. Always verify the index is within0..<array.countbefore assignment. - Trying to modify a
letarray: Arrays declared withletare immutable.let arr = ["A"]; arr[0] = "B"produces a compile error. Usevarfor arrays that need modification. - Confusing
firstIndex(of:)withindex(of:): In modern Swift,firstIndex(of:)is the correct method. The olderindex(of:)was renamed. Both returnOptional<Int>— always unwrap before using as a subscript. - Mutating during
for-initeration:for item in array { array.remove(...) }causes undefined behavior or crashes. Useindiceswithwhereclause, iterate in reverse, or build a new array withfilter/map. - Not handling the case where the element is not found:
firstIndex(of:)returnsnilif the element does not exist. Force-unwrapping the result (array[cities.firstIndex(of: "Missing")!]) crashes. Always useif letorguard let.
Summary
- Replace by index with
array[index] = newValue— the most common pattern - Always check bounds before subscript access to avoid crashes
- Use
array[range] = newElementsorreplaceSubrangefor bulk replacements - Use
swapAt(i, j)to swap two elements efficiently - Use
firstIndex(of:)to find an element before replacing it - Use
mapfor non-mutating transformations that produce a new array
Related reading
- Swift Set to Array
- Swift Sort array of objects alphabetically
- Swift Sort array of objects alphabetically
- Swift stack and heap understanding
- Swift stack and heap understanding
- Swift startsWith method?
- Swift what is the right way to split up a String resulting in a String with a given subarray size?
- SwiftUI - Multiple Buttons in a List row

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.