warning 'characters' is deprecated Please use String or Substring directly
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Swift warning about characters being deprecated means the language no longer wants you to access a separate characters view on String. Modern Swift treats String itself as a collection of Character values, so most old code can be simplified rather than replaced with a new special API.
In practice, the fix is usually mechanical: remove .characters and operate directly on the String or Substring. The only part worth thinking about is whether your code should keep a slice as Substring or convert it back to String.
Why characters Went Away
Older Swift versions had separate views such as myString.characters. That design made string handling noisier and harder to teach. Since Swift 4, strings themselves conform to collection protocols in a way that lets you iterate over characters directly.
So instead of writing this old style:
You now write:
That is the main meaning of the deprecation warning.
Common Replacements
A few old patterns show up repeatedly in legacy code.
Counting characters:
Checking emptiness:
Iterating over characters:
Converting a character sequence back into a string is also straightforward:
All of these replace old .characters-based code with direct String usage.
Substring Is the Other Half of the Change
When you slice a string in modern Swift, the result is usually a Substring, not a new String. That is good for performance because it avoids copying immediately.
If another API requires a String, convert explicitly:
That is why the warning mentions String or Substring directly. The language wants you to use the real types involved rather than an older compatibility layer.
Character Handling in Swift Is Unicode-Aware
One reason Swift string APIs can feel different from some other languages is that a Character is not just one byte. Swift characters are Unicode-aware and can represent extended grapheme clusters.
That means operations such as counting or indexing characters are based on user-visible characters, not raw UTF-8 bytes. This is a good thing, but it also means you should avoid assumptions like "the fifth byte is the fifth character."
The deprecation of .characters did not remove Character as a concept. It just removed the extra collection wrapper around it.
Updating Legacy Code Safely
For most codebases, migration is a simple search-and-replace followed by a type check.
Typical rewrites:
- '
str.characters.countbecomesstr.count' - '
for c in str.charactersbecomesfor c in str' - '
String(str.characters.prefix(3))becomesString(str.prefix(3))'
Example:
After the mechanical cleanup, review any APIs that return slices. If a value needs to outlive the original string or be stored independently, convert the Substring to String explicitly.
Common Pitfalls
The main mistake is trying to find a direct replacement property for .characters instead of noticing that String already does the job. Another is forgetting that slices return Substring, which can cause type mismatches when code expects String. Developers also still make byte-based assumptions about character counts, which leads to bugs with emoji and accented characters. The warning is easy to silence, but the real goal is to update your mental model of how Swift strings work.
Summary
- The
.charactersview was deprecated becauseStringitself is a collection ofCharactervalues. - Most old code can be fixed by removing
.charactersdirectly. - Use
count, iteration, and collection methods onStringitself. - String slices are usually
Substring, and you can convert them withString(...)when needed. - Remember that Swift string operations are Unicode-aware, not byte-based.

