How to convert Index to type Int in Swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Swift, not every collection index is an Int. Arrays use integer indices, but types such as String use custom index types for safety. So the right answer depends on which collection you are working with and whether you really need an integer offset at all.
Arrays Already Use Int
For arrays, the index type is already integer-based:
There is no special conversion step here because Array.Index is effectively Int.
Strings Are Different
String does not use plain integers for indexing because characters can have variable-width encoding. A String.Index is a position object, not a raw numeric offset.
You cannot safely cast that index directly to Int. Instead, compute the distance from startIndex.
Convert a String.Index to an Integer Offset
Use distance(from:to:):
This gives you the integer offset from the start of the string to that position.
Convert Back from Int to a String.Index
If you later need the reverse operation, use index(_:offsetBy:):
These two operations are the normal bridge between integer offsets and string indices in Swift.
Why Swift Does Not Expose Integer String Indices Directly
Swift avoids raw integer indexing for strings because a visible character is not always one byte or even one Unicode scalar. Direct integer indexing would invite invalid assumptions about character boundaries.
That is why this does not exist as a safe language feature:
The collection-specific index type protects you from splitting characters incorrectly.
Sometimes You Should Keep the Native Index
If the value will be used again with the same collection, keeping the native index is often better than converting it to an Int. Converting to an offset and back repeatedly may be less clear and can cost extra work on collections like strings.
So ask first whether you need:
- an offset for display or another API
- or the original collection index for more collection operations
The right representation depends on how the value will be used next.
Common Pitfalls
The biggest pitfall is assuming all Swift collection indices are integers. That is true for arrays, but not for strings and many other collections.
Another common mistake is trying to cast String.Index directly to Int. The correct approach is to compute distance from a known anchor such as startIndex.
People also forget that integer offsets for strings are meaningful only relative to the same string contents. If the string changes, a previously computed offset may no longer point to the same character.
That is one more reason to keep the native index whenever the value will stay inside Swift collection code.
Summary
- '
Array.Indexis already anInt, so no special conversion is needed.' - '
String.Indexis not anIntand should be converted withdistance(from:to:)when needed.' - Convert an integer offset back to a string index with
index(_:offsetBy:). - Swift uses custom string indices to preserve Unicode correctness.
- Keep the native index when possible instead of converting unnecessarily.

