Your Cache Key Says "user-Optional(42)". Swift String Interpolation Wrote It, and Xcode's Fix-It Made It Permanent.
The cache hit rate on the profile screen was zero. Not low. Zero. Every single launch, every single user, the app went to the network for an avatar it had downloaded a hundred times before.
I opened the cache folder in the simulator and found two hundred files named avatar-Optional(42).jpg. The reader was looking for avatar-42.jpg. The writer had been helpfully labeling everything with a type name nobody asked for.
The one-line bug
Here’s what the writer looked like:
struct User {
let id: Int
let teamID: Int?
}
func cacheKey(for user: User?) -> String {
"avatar-\(user?.id).jpg"
}
user is optional, so user?.id is Int?, not Int. And when you interpolate an optional into a string, Swift doesn’t unwrap it. It calls the debug description. You get "avatar-Optional(42).jpg" when there’s a user and "avatar-nil.jpg" when there isn’t.
The reader, written by a different person on a different day, took a plain User and produced "avatar-42.jpg". Both functions are correct on their own. Together they’re a cache that never hits.
Optional chaining is the usual culprit. Nobody writes "\(someOptional)" on purpose. They write user?.id, the ? quietly turns the whole expression optional, and the string picks it up without complaint.
Where “Optional(” ends up
Once you start grepping production logs for it, it shows up everywhere strings meet the outside world.
URLs. "/users/\(session?.userID)/orders" becomes /users/Optional(42)/orders. Your server returns a 404 and your app shows “Something went wrong.” (URL building has other traps too, like the plus sign that turns into a space.)
Cache and dictionary keys. Like my avatars. The write and the read disagree on the key, so nothing crashes and nothing is found. It’s the quiet cousin of duplicate keys crashing a dictionary: same root cause (two places disagreeing about what a key is), opposite symptom.
File names. "export-\(document.title).pdf" on an optional title gives your users a file literally called export-nil.pdf in the Files app. Store that name in a database and you’ve also got the problem from saving file paths that break later.
Analytics. "screen_\(route?.name)" creates a brand-new event name in your dashboard. Your product manager asks why “Checkout” traffic dropped 100% the same week a new event called screen_Optional("checkout") appeared from nowhere. Note the escaped quotes: a String? debug description includes them.
UserDefaults keys. Same story as the cache. The setting saves fine, it just saves somewhere nobody reads.
”But the compiler warns about this”
It does. Write "\(user?.id)" and you get:
String interpolation produces a debug description for an optional value; did you mean to make this explicit?
That warning is good. The trouble is what happens next.
First, it’s one yellow triangle among however many your project already has. If you have 200 warnings, you have 0 warnings, because nobody reads them.
Second, and this is the part that actually got me: the fix-its. Xcode offers to “Use ‘String(describing:)’ to silence this warning” or to “Provide a default value to avoid this warning”. Someone in a hurry clicks the first one, because it doesn’t require a decision. The code becomes:
"avatar-\(String(describing: user?.id)).jpg"
The warning is gone. The output is identical: still avatar-Optional(42).jpg. You’ve taken a bug the compiler was pointing at and made it invisible to the compiler forever. That’s not a fix, that’s putting tape over the check-engine light.
The same goes for "\(value as Any)", the other popular silencer. Same output, no warning.
Third, the warning doesn’t cover everything. Pass an optional through a generic or Any parameter first and the compiler has nothing to complain about:
func log(_ parts: Any...) -> String {
parts.map { "\($0)" }.joined(separator: " ")
}
log("user", user?.id) // "user Optional(42)", no warning
Inside log, $0 is Any. As far as the compiler knows, it’s just a value. The Optional( gets baked in silently.
The fix: decide what nil means
The real reason this bug exists is that nobody decided what should happen when the value is missing. Interpolation made the decision for you, and its decision was “print some debug text.”
So make the decision. There are really only three answers.
1. Nil is impossible here. Unwrap before you build the string.
func cacheKey(for user: User) -> String {
"avatar-\(user.id).jpg"
}
// At the call site:
guard let user else { return placeholderAvatar }
let key = cacheKey(for: user)
This is the best answer most of the time. Push the optional out of the string-building function entirely. If a function’s job is “make a key for a user,” it shouldn’t accept “maybe a user.”
2. Nil means “don’t do this at all.”
func ordersURL(for userID: Int?) -> URL? {
guard let userID else { return nil }
return baseURL.appending(path: "users/\(userID)/orders")
}
A request for “nobody’s orders” shouldn’t hit the network. Return nil and let the caller show the signed-out state.
3. Nil has a real, meaningful fallback.
let label = "Team: \(user.teamID.map(String.init) ?? "none")"
Use ?? only when the fallback is something you’d be happy to see in production. ?? "" in a URL path is not that. It turns /users/42/orders into /users//orders, which is just a different 404.
Make the fallback explicit with a custom interpolation
If your codebase genuinely has lots of “optional with a display fallback” strings, you can teach string interpolation a new trick. Swift lets you extend String.StringInterpolation:
extension String.StringInterpolation {
mutating func appendInterpolation<T>(
_ value: T?,
default defaultValue: @autoclosure () -> String
) {
if let value {
appendInterpolation(value)
} else {
appendLiteral(defaultValue())
}
}
}
let teamID: Int? = nil
let label = "Team: \(teamID, default: "none")" // "Team: none"
let other: Int? = 7
let label2 = "Team: \(other, default: "none")" // "Team: 7"
The nice thing about this is that the fallback lives right inside the string, where the reader can see it, and it’s impossible to write without making a decision. It doesn’t suppress anything: plain "\(teamID)" still warns. You’re adding a good path, not hiding the bad one.
I wouldn’t use this in URLs or keys. Those should be answer 1 or 2. It’s for labels and log lines.
Make it impossible to ship
Fixing the one avatar bug is easy. Not writing the next one is the real job.
Promote warnings to errors, at least in packages you control. If your features live in Swift packages, SwiftPM on recent toolchains (Swift 6.2 and later) lets you add .treatAllWarnings(as: .error) to a target’s swiftSettings. In an Xcode target, the build setting is “Treat Warnings as Errors” (SWIFT_TREAT_WARNINGS_AS_ERRORS). Turn it on for one module, clean it up, and spread it from there. A warning that can’t ship gets fixed instead of fixit-ed.
Grep for the silencers. This finds the bugs that already made it past the compiler:
grep -rnE 'String\(describing:|as Any\)' --include='*.swift' Sources/ App/
Every hit deserves a look. Most will be in debug logging, which is fine. The ones inside URLs, keys, and file names are the bugs.
Grep your production logs, too. Search for Optional( and -nil, /nil/, _nil in your request logs, crash breadcrumbs, and analytics event names. If your backend team has ever asked “why do some requests have Optional in the path,” now you know.
Test the string builders. Any function that produces a key, a path, or an event name gets a test that checks it never leaks Swift’s debug text:
import Testing
@Suite struct StringBuilderTests {
@Test(arguments: [
User(id: 42, teamID: nil),
User(id: 7, teamID: 3),
])
func keysAndPathsNeverContainDebugText(user: User) {
let outputs = [
cacheKey(for: user),
ordersURL(for: user.id)?.absoluteString ?? "",
]
for output in outputs {
#expect(!output.contains("Optional("), "Leaked debug description: \(output)")
#expect(!output.contains("nil"), "Leaked nil: \(output)")
}
}
}
The "nil" check is a bit blunt. If you have a user named Nils, refine it. But for keys and paths built from IDs, blunt is exactly right.
Why this one is worth an afternoon
This bug never crashes. It never shows up in a debugger session either, because when you po the value, Optional(42) looks perfectly normal. You only find it by noticing that a number (a cache hit rate, an event count, a success rate) is wrong, and then working backward for an hour.
That’s also why it’s a good one to hand to an AI assistant with real context. “Our cache hit rate is zero, here are the read and write functions” gets you to the answer fast. “Fix the warnings” might get you String(describing:) everywhere, which is how I got here in the first place. (More on giving AI tools the right context in the debugging with AI lesson.)
The deeper lesson: every optional going into a string needs an answer to “what if it’s missing?” Swift’s default answer is a debug description, because it has no idea what you want. That’s a reasonable default for print. It’s a terrible one for anything another system has to read.
I renamed the two hundred avatar files with a one-off migration, fixed the key function to take a non-optional User, and the hit rate went from 0% to 94% overnight. The warning had been sitting there for months. Nobody had ignored it. Somebody had clicked the fix-it.
Share this post
Comments
Leave a comment
NativeFirst Team
EditorialThe NativeFirst team — engineers and designers building native Apple apps and writing the courses we wish we had when we started.