Reputation: 31323
I found the following code snippet in this sample project. It takes a [String: AnyObject]
dictionary, escapes the spacial characters and creates a parametrized string.
class func escapedParameters(parameters: [String : AnyObject]) -> String {
var urlVars = [String]()
for (key, value) in parameters {
let stringValue = "\(value)"
let escapedValue = stringValue.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
urlVars += [key + "=" + "\(escapedValue!)"]
}
return (!urlVars.isEmpty ? "?" : "") + join("&", urlVars)
}
The original code is a class function. I wanted to make this an extension method of the Dictionary type.
extension Dictionary {
func escapedParameters() -> String {
var urlVars = [String]()
for (key, value) in self {
let stringValue = "\(value)"
let escapedValue = stringValue.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
urlVars += [key + "=" + "\(escapedValue!)"]
}
return (!urlVars.isEmpty ? "?" : "") + join("&", urlVars)
}
}
But I get the following error at this line urlVars += [key + "=" + "\(escapedValue!)"]
.
Could not find member 'init'
I can't figure out why or how to correct this. This issue doesn't occur when it's a class function.
Upvotes: 0
Views: 463