I am using AFNetworking in my iOS app and for all the GET requests it makes, I build the url from a base URL and than add parameters using NSDictionary Key-Value pairs.
The problem is that I need same key for different values.
Here is an example of what I need the finally URL to look like -
http://example.com/.....&id=21212&id=21212&id=33232
It's not possible in NSDictionary to have different values in same keys. So I tried NSSet but did not work.
let productIDSet: Set = [prodIDArray]
let paramDict = NSMutableDictionary()
paramDict.setObject(productIDSet, forKey: "id")
All you need is
NSURLComponents
. The basic idea is to create a bunch of query items for your id's. Here's code you can paste into a playground:You should see an output of
2019
Percent-encoding and everything else is handled.
Extending on the extension ideas above, this was more useful in my case:
USAGE:
RESULT:
I guess u just have to do something like this:
which will be encoded into: ....id%5B%5D=1&id%5B%5D=2&id%5B%5D=3&id%5B%5D=4....
An URL extension to append query items, similar to Bhuvan Bhatt idea, but with a different signature:
nil
instead ofself
), thus allowing custom handling of cases where the URL is not RFC 3986 compliant for instance.Usage
Output: