Created
July 20, 2015 23:20
-
-
Save nolim1t/fd10bed0cacb343f2b23 to your computer and use it in GitHub Desktop.
Save the contents of my swift playground
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
func escape(string: String) -> String { | |
let generalDelimiters = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4 | |
let subDelimiters = "!$&'()*+,;=" | |
let legalURLCharactersToBeEscaped: CFStringRef = generalDelimiters + subDelimiters | |
return CFURLCreateStringByAddingPercentEscapes(nil, string, nil, legalURLCharactersToBeEscaped, CFStringBuiltInEncodings.UTF8.rawValue) as String | |
} | |
println(escape(phnumber)) | |
func queryComponents(key: String, _ value: AnyObject) -> [(String, String)] { | |
var components: [(String, String)] = [] | |
if let dictionary = value as? [String: AnyObject] { | |
for (nestedKey, value) in dictionary { | |
components += queryComponents("\(key)[\(nestedKey)]", value) | |
} | |
} else if let array = value as? [AnyObject] { | |
for value in array { | |
components += queryComponents("\(key)[]", value) | |
} | |
} else { | |
components.extend([(escape(key), escape("\(value)"))]) | |
} | |
return components | |
} | |
func query(parameters: [String: AnyObject]) -> String { | |
var components: [(String, String)] = [] | |
for key in sorted(Array(parameters.keys), <) { | |
let value: AnyObject! = parameters[key] | |
components += queryComponents(key, value) | |
} | |
return join("&", components.map{"\($0)=\($1)"} as [String]) | |
} | |
var q = ["xy": "z", "a": "b", "de": "fg"] | |
var s = query(q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment