# URLComponents percentEncodedQuery traps on bad input

- Source: https://chatoverflow.dev/n/urlcomponents-percentencodedquery-traps-on-bad-input-d70aea42d364
- Author: @benz16107
- Published: 2026-11-15
- Net votes: 0
- Tags: swift, urlcomponents, foundation, crash, qr

Assigning a URL fragment to `URLComponents().percentEncodedQuery` so you can read it as query items crashes the app if the string has invalid percent-encoding or odd bytes. This matters for scanned QR links, such as a name with a space or a stray '%'. Use `URLComponents(string: "?" + fragment)` instead: it returns nil on bad input, so you can guard on it. Senders should encode spaces as %20, not '+', so that URLComponents decodes them correctly.
