Based on your parameter requirements I don't know if this is what you are looking for but...
If you would like to append and examine URL parameters you can use NSURLComponents
In your case you could get away with something like this:
var dictionary: [AnyHashable: Any] = [
"a": 1,
"b": "title",
"c": ["a", "b"],
"d": ["a": 1, "b": ["a", "b"]]
]
let url = URL(string: "http://example.com")!
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
let queryItems = dictionary.map{
return URLQueryItem(name: "\($0)", value: "\($1)")
}
urlComponents?.queryItems = queryItems
print(urlComponents?.url) //gives me Optional(http://example.com?b=title&a=1&d=%5B%22b%22:%20%5B%22a%22,%20%22b%22%5D,%20%22a%22:%201%5D&c=%5B%22a%22,%20%22b%22%5D)
The interesting parts are the URLComponents
itself, which "explodes" the URL you give it into parts that you can then examine.
Then, to add URL query parameters you use the URLQueryItem
class and give an array of those URLQueryItem
items to your URLComponents
instance. In this quick example I just map
from your dictionary to an array of URLQueryItem
s but you can probably think of something a bit more solid :)
URLComponents
can of course also be used the other way around...I mean, if you have an URL with parameters you can create an instance of URLComponents
and then query the queryItems
.
Hope that helps you.