How can I build a URL with query parameters containing multiple values for the same key in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Some APIs accept repeated query keys such as tag=swift&tag=ios instead of a single comma-separated value. In Swift, the safest way to build that kind of URL is with URLComponents, because it preserves repeated keys and handles percent encoding correctly.
Use Repeated URLQueryItem Values
URLComponents stores query parameters as an array of URLQueryItem, not as a dictionary. That matters because dictionaries cannot represent repeated keys without losing information.
Here is the basic pattern:
The resulting URL is:
Because each repeated value is its own URLQueryItem, the final query string keeps both tag entries in order.
Build Repeated Parameters from Arrays
In real code, repeated query keys often come from an array such as selected filters or category ids. A helper function makes the construction predictable:
This keeps the repeated-key behavior explicit and avoids manual string concatenation.
Do Not Use a Dictionary for Repeated Keys
A common first attempt is something like this:
That does not work the way people expect, because dictionary keys must be unique. One of the values wins and the other is lost. If the API requires repeated keys, use an ordered array of URLQueryItem instead.
Match the Server’s Expected Format
Not every backend expects the same syntax. Many APIs accept repeated keys:
Others want bracketed names:
If the server expects the bracketed form, keep the repeated structure but change the item name:
The important point is that the repetition happens through multiple query items, not through a single combined string unless the API documentation explicitly says otherwise.
Let Foundation Handle Encoding
URLComponents is also valuable because it encodes reserved characters correctly. If a filter value contains spaces, punctuation, or symbols, Foundation handles the escaping for you:
Manual string building often gets this wrong, especially once spaces, ampersands, or plus signs appear in the values.
Common Pitfalls
The biggest pitfall is using a dictionary to represent query parameters when the API allows repeated keys. Dictionaries are the wrong data structure for that job.
Another common mistake is building the query string manually with string interpolation. That works for simple cases, then breaks as soon as a value needs percent encoding.
It is also easy to assume every backend parses arrays the same way. Some expect repeated keys, some expect bracketed names, and others expect a comma-separated string. Check the API contract before choosing the format.
Finally, remember that components.url is optional. If the base URL is invalid, the final URL will be nil, so treat construction as a potentially failing step.
Summary
- Use
URLComponentswith multipleURLQueryItementries to represent repeated query keys. - Do not use a dictionary when duplicate keys must be preserved.
- Build repeated items from arrays to keep the code clear and maintainable.
- Match the exact query syntax expected by the server, whether repeated keys or bracketed names.
- Let Foundation handle percent encoding instead of building query strings by hand.
Related reading
- How can I call this async method in my Xamarin Forms when my app starts?
- How can I change a SwiftUI Color to UIColor?
- How can I change default dialog button text color in android 5
- How can I change image tintColor in iOS and WatchKit
- How can I change image tintColor in iOS and WatchKit
- How can I change locale programmatically with Swift
- How can I change the app display name build with Flutter?
- How can I change the app display name build with Flutter?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.