Treat Unicode Text And Server Identifiers Differently
Here is a small Swift surprise: two strings can compare equal, contain the same number of characters, and still have different UTF-8 bytes.
That is useful for human text. It deserves a closer look when the string is an opaque identifier from another system.
Imagine importing two records into a dictionary. The backend considers their identifiers different, but Swift considers the string keys equal. One record replaces the other before SwiftUI ever gets a chance to render it. The bug looks like a missing row; the actual disagreement is about identity.
See the difference in a few lines
These strings spell the same word using two Unicode representations:
let composed = "caf\u{00E9}"
let decomposed = "cafe\u{0301}"
assert(composed == decomposed)
assert(composed.count == decomposed.count)
assert(Array(composed.utf8) != Array(decomposed.utf8))
assert(Set([composed, decomposed]).count == 1)
Swift’s Strings and Characters guide describes equality in terms of canonical equivalence. A precomposed accented character and a base character followed by its combining accent can compare equal.
This does not mean every visually similar character is equal. A lookalike letter from another script is a different issue. It also does not mean comparison is case-insensitive.
For display names and ordinary text, do not replace Swift’s behavior with byte comparison as a blanket “fix.” First determine what equality is supposed to mean for this particular field.
Ask what the protocol promises
The identifier contract should answer these questions:
| Contract | Appropriate starting point |
|---|---|
| A UUID in a textual representation | Parse it as UUID and use its identity. |
| An ASCII token with specified case sensitivity | Validate the alphabet and preserve the required case behavior. |
| Human text with specified normalization | Apply the agreed normalization at the agreed boundary. |
| Opaque text whose UTF-8 bytes define identity | Preserve and compare those bytes explicitly. |
The last row is a specific contract, not a claim about every API that returns a JSON string. If the service has not defined comparison rules, agree on them before introducing a client-only interpretation.
Avoid quietly calling lowercased(), trimming whitespace, or removing accents from an opaque ID. Those operations may create a different identifier even when they make a display name look nicer.
Put byte identity in a separate type
For a service that explicitly defines identifiers by their UTF-8 bytes, this small wrapper makes that behavior visible:
struct OpaqueUTF8ID: Hashable, Sendable, Codable {
private let bytes: [UInt8]
init(_ value: String) {
bytes = Array(value.utf8)
}
var stringValue: String {
String(decoding: bytes, as: UTF8.self)
}
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.init(try container.decode(String.self))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(stringValue)
}
}
The stored representation drives synthesized equality and hashing. The transport representation remains a JSON string, so we do not accidentally change the service payload into an array of integers or base64 data.
Now our two IDs remain distinct:
let first = OpaqueUTF8ID(composed)
let second = OpaqueUTF8ID(decomposed)
assert(first != second)
assert(Set([first, second]).count == 2)
Use that type throughout the boundary that depends on the contract: decoded record IDs, dictionary keys, selected IDs, and the ID exposed to SwiftUI. Converting back to String for a dictionary key would reintroduce the original comparison behavior.
The wrapper preserves the UTF-8 representation of the decoded string. It does not preserve the original JSON document bytes: a JSON escape sequence and a literal character can decode to the same value. If a protocol signs raw payload bytes, retain and verify the raw payload according to that protocol instead of re-encoding a decoded model.
Keep display and identity apart
A record can have both an opaque ID and a friendly title:
struct ImportedRecord: Identifiable, Decodable {
let id: OpaqueUTF8ID
let title: String
}
The title can use the app’s normal localized search and display behavior. The ID only needs to round-trip correctly and select the right record. Making one string do both jobs is what creates pressure to normalize an identifier for cosmetic reasons.
The same distinction applies to length limits. A limit expressed in UTF-8 bytes should inspect utf8.count; a user-facing character count usually has a different purpose. For example, a family emoji can be one Swift Character while occupying several Unicode scalars and many bytes. Name the unit in the validation rule.
Test the round trip
September 2026 review: the executable Swift 6.4 sample checks canonical string equality, distinct byte-based set membership, case preservation, and JSON encode/decode round trips for both representations. It also checks the single-character family-emoji example.
Add an integration fixture from the actual service before adopting this type. Verify that a decoded ID survives persistence, selection, request construction, and the server’s response unchanged under its stated comparison rules. Include a duplicate-key fixture so a future refactor cannot silently collapse distinct records.
Most apps will never need a custom byte-based text identifier. The valuable habit is to ask what a string represents before using it as identity. Human text and a server’s opaque key can look identical in the debugger while making very different promises.