Swift
Index
Int
Type Conversion
Programming

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:

swift
1let values = [10, 20, 30]
2let index = values.startIndex
3
4let intIndex: Int = index
5print(intIndex)  // 0

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.

swift
let text = "Hello"
let index = text.index(text.startIndex, offsetBy: 2)
print(text[index])  // l

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:):

swift
1let text = "Hello"
2let index = text.index(text.startIndex, offsetBy: 2)
3
4let offset = text.distance(from: text.startIndex, to: index)
5print(offset)  // 2

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:):

swift
1let text = "Hello"
2let offset = 2
3
4let index = text.index(text.startIndex, offsetBy: offset)
5print(text[index])  // l

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:

swift
// text[2]  // not valid Swift string indexing

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.Index is already an Int, so no special conversion is needed.'
  • 'String.Index is not an Int and should be converted with distance(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.

Course illustration
Course illustration

All Rights Reserved.