Last active
March 13, 2018 10:44
-
-
Save jaderfeijo/85abac17473c545412319d0dc7cd8671 to your computer and use it in GitHub Desktop.
URL+QueryStrings.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
import Foundation | |
extension URL { | |
var hasQueryStringParameters: Bool { | |
return query != nil | |
} | |
var queryStringParameters: [String: String] { | |
var results = [String: String]() | |
if let keyValues = query?.components(separatedBy: "&"), keyValues.count > 0 { | |
for pair in keyValues { | |
let kv = pair.components(separatedBy: "=") | |
if kv.count > 1 { | |
let key = kv[0].urlDecoded | |
let value = kv[1].urlDecoded | |
results.updateValue( | |
value, | |
forKey: key | |
) | |
} | |
} | |
} | |
return results | |
} | |
} | |
// MARK: - Private - | |
extension String { | |
fileprivate | |
var urlDecoded: String { | |
return removingPercentEncoding ?? self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment