Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolim1t/fd10bed0cacb343f2b23 to your computer and use it in GitHub Desktop.
Save nolim1t/fd10bed0cacb343f2b23 to your computer and use it in GitHub Desktop.
Save the contents of my swift playground
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