Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
get the value of a specific query item within NSURL
extension NSURL {
func getQueryItemValueForKey(key: String) -> String? {
guard let components = NSURLComponents(URL: self, resolvingAgainstBaseURL: false) else {
return nil
guard let queryItems = components.queryItems else { return nil }
return queryItems.filter {
$ == key
Copy link

vinod1879 commented May 10, 2016

This will fail if the query-param key has a case mismatch.
I would suggest improving it to:
$ == key.lowercaseString

Copy link

InsertNetan commented Jun 24, 2016

I don't know if you always want a case insensitive check for your keys.
you can add extra parameter with a default value for case sensitive/insensitive check.
func getQueryItemValueForKey(key: String, isCasesensitive casesensitive: Bool = false ) -> String?
and apply the correct logic in the body of the function

Copy link

danipralea commented Dec 23, 2016

very nice. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment