swift convert RangeInt to Int
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
A Range<Int> in Swift is not a single integer — it represents a sequence of integers between a lower and upper bound. Converting it to an [Int] array uses Array(range). To extract a single integer from a range, access specific properties like lowerBound, upperBound, or count. The conversion approach depends on whether you need all values in the range, just the bounds, or a random element.
Range to Array
Array(range) iterates over the range and collects each integer into an array.
Accessing Bounds
Random Element from Range
Mapping a Range
Ranges conform to Sequence, so map, filter, and reduce work directly without converting to an array first.
Iterating Over a Range
Range with Collection Subscripts
Converting Between Range Types
Using Ranges with String Indices
NSRange and Swift Range Conversion
Checking if a Value Is in a Range
Common Pitfalls
- Empty ranges:
5..<5is an empty range (no elements).Array(5..<5)returns[].5..<4is invalid and crashes at runtime. - ClosedRange vs Range:
1...5has 5 elements;1..<5has 4. Off-by-one errors are the most common range bug. - Large ranges:
Array(0..<1_000_000_000)allocates 8 GB of memory (1 billion Int64 values). Use ranges directly inforloops orlazysequences instead of converting to arrays. - String indices are not Int: Swift strings use
String.Index, notInt. You cannot writestr[0..<5]. Usestr.index(str.startIndex, offsetBy: n)to convert. - Range is not RandomAccessCollection for all types:
Range<Int>supports random access.Range<String.Index>does not — you cannot jump to an arbitrary position without iterating.
Summary
- Use
Array(range)to convert aRange<Int>to[Int] - Access
.lowerBound,.upperBound, and.countto extract integers from a range - Use
Int.random(in: range)for a random integer from a range - Ranges support
map,filter,reduce, andfor-indirectly without array conversion - Use
stride(from:to:by:)for ranges with custom step sizes - Convert between
RangeandNSRangewithNSRange(_:in:)andRange(_:in:)
Related reading
- Swift declare an empty dictionary
- Swift declare an empty dictionary
- Swift delete all array elements
- Swift delete all array elements
- Swift convert unix time to date and time
- Swift. Could not build objective-c module 'Alamofire
- Swift Dictionary Get values as array
- Swift JSONDecode decoding arrays fails if single element decoding fails

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.