Firstly, the the block or closure you're providing to the map function isn't quite right. Blocks are basically nameless functions, they take some number of parameters and return some type. If we were to be verbose, a solution would look something like this:
params.map { (a: (String, String)) -> NSURLQueryItem in
return NSURLQueryItem(name: a.0, value: a.1)
}
However we can simplify this bit of code. The dictionary is [String : String]
so it can be inferred that the map function will take a (String, String)
as a parameter, so we don't need to write that explicitly.
Swift also allows $n
to refer to the nth element of a tuple parameter. Since we only have 1 parameter, $0
will refer to the first element of the first parameter.
The return type of the block can also be inferred, since we're creating a NSURLQueryItem
, meaning we don't need to specify that either. This means we also don't need to write return
in the block, we can just create the object.
Lastly, you should not call NSURLQueryItem.init()
to create a new NSURLQueryItem
, you should instead just say NSURLQueryItem()
.
Here's the minimal solution:
params.map { NSURLQueryItem(name: $0, value: $1) }