Skip to content

Instantly share code, notes, and snippets.

@g-mark
Created February 16, 2018 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g-mark/e81dcbe6968dfdad9f1a8cc0e848bb71 to your computer and use it in GitHub Desktop.
Save g-mark/e81dcbe6968dfdad9f1a8cc0e848bb71 to your computer and use it in GitHub Desktop.
URLComponents Example
let pageIndex = 2
// base url
var components = URLComponents(string: "https://hostname.com/path")
// build query items as a String: String dictionary (easire to deal with that URLQueryItems)
var items: [String: String] = ["page": "\(pageIndex)", "message": "Hi mom & family!"]
items["fields"] = "_id,versionId,sourceType,name,_displayName,points"
components?.queryItems = items.map { URLQueryItem(name: $0.key, value: $0.value) }
print(components?.url)
// "https://hostname.com/path?fields=_id,versionId,sourceType,name,_displayName,points&page=2&message=Hi%20mom%20%26%20family!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment