Skip to content

Instantly share code, notes, and snippets.

@Gujci
Created April 23, 2016 20:56
Show Gist options
  • Save Gujci/0f6cbcab4257b78c0643de571e7931fd to your computer and use it in GitHub Desktop.
Save Gujci/0f6cbcab4257b78c0643de571e7931fd to your computer and use it in GitHub Desktop.
extension NSURL {
convenience init(string: String, query: Dictionary<String, String>?) {
if query == nil {
self.init(string: string)!
return
}
let components = NSURLComponents(string: string)
var querryItems = components?.queryItems ?? Array<NSURLQueryItem>()
query?.forEach() {
querryItems.append(NSURLQueryItem(name: $0.0, value: $0.1))
}
components?.queryItems = querryItems
self.init(string: "",relativeToURL: components!.URL)!
}
convenience init(url: NSURL, query: Dictionary<String, String>?) {
let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: false)
var querryItems = components?.queryItems ?? Array<NSURLQueryItem>()
query?.forEach() {
querryItems.append(NSURLQueryItem(name: $0.0, value: $0.1))
}
components?.queryItems = querryItems
self.init(string: "",relativeToURL: components!.URL)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment