Created
February 16, 2018 16:01
-
-
Save g-mark/e81dcbe6968dfdad9f1a8cc0e848bb71 to your computer and use it in GitHub Desktop.
URLComponents Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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