I am in process of learning swift and web services.
When I tried to compile the following code in IOS 9, I get
reduce is unavailable: call the reduce() method on the sequence
private class func urlWithName(name: String, var args: [String: String]) -> NSURL
{
args["username"] = "ijoshsmith"
let
baseURL = "http://api.geonames.org/",
queryString = queryWithArgs(args),
absolutePath = baseURL + name + "?" + queryString
return NSURL(string: absolutePath)!
}
private class func queryWithArgs(args: [String: String]) -> String
{
let parts: [String] = reduce(args, [])
{
result, pair in
let
key = pair.0,
value = pair.1,
part = "\(key)=\(value)"
return result + [part]
}
return (parts as NSArray).componentsJoinedByString("&")
}
So, I understand that I should covert reduce(args, []) to something like args.reduce(initial: T, combine:(T, Self.Generator.Element) throws -> T). Can someone help me in explaining and converting it.
thanks.