Last active
November 13, 2019 05:24
-
-
Save naveedmcs/fb785c2f8ce6e5f4f6fbdd5ca28fc224 to your computer and use it in GitHub Desktop.
Query String Params handling in swift
This file contains hidden or 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
| //"Query Params Example- by nvd" | |
| extension URL { | |
| /// Returns a new URL by adding the query items, or nil if the URL doesn't support it. | |
| /// URL must conform to RFC 3986. | |
| func appending(_ queryItems: [URLQueryItem]) -> URL? { | |
| guard var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true) else { | |
| // URL is not conforming to RFC 3986 (maybe it is only conforming to RFC 1808, RFC 1738, and RFC 2732) | |
| return nil | |
| } | |
| // append the query items to the existing ones | |
| urlComponents.queryItems = (urlComponents.queryItems ?? []) + queryItems | |
| // return the url from new url components | |
| return urlComponents.url | |
| } | |
| } | |
| let baseURL = URL(string: "https://example.com/")! | |
| let queryItems = [URLQueryItem(name: "page", value: nil), | |
| URLQueryItem(name: "keyword", value: "emglish tarzan"), | |
| URLQueryItem(name: "id", value: "33")] | |
| let completeURL = baseURL.appending(queryItems)! | |
| let completeURLString = "\(completeURL)" | |
| print(completeURLString) | |
| //https://example.com/?page&keyword=emglish%20tarzan&id=33 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment