Skip to content

Replace force cast with safe conditional cast#455

Open
jcarroll3 wants to merge 1 commit intodropbox:masterfrom
jcarroll3:remove-implicitly-unwrapped-optionals
Open

Replace force cast with safe conditional cast#455
jcarroll3 wants to merge 1 commit intodropbox:masterfrom
jcarroll3:remove-implicitly-unwrapped-optionals

Conversation

@jcarroll3
Copy link

Description:

caseInsensitiveLookup iterates over an [AnyHashable: Any] dictionary (from HTTPURLResponse.allHeaderFields) and force-casts each key to String with as!. While HTTP header keys are typically strings, AnyHashable can technically hold non-string keys, and a force cast crash in production is never acceptable in an SDK consumed by third-party apps.

This change uses guard let keyString = key as? String else { continue } to safely skip any non-string keys instead of crashing.

Why it should land: A force unwrap crash in a public SDK is a serious reliability risk. Even if non-string header keys are rare, the safe cast prevents a potential crash in consumer apps. This follows Swift best practices for defensive coding in library code.

@jcarroll3 jcarroll3 force-pushed the remove-implicitly-unwrapped-optionals branch from fe5dab8 to 74ea4a7 Compare February 25, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant