I am using this flatmap function to return a dictionary but it is returning [(key: String, value: String)]
instead. What is the difference between [(key: String, value: String)]
and a dictionary? Here is the code I am using. What would be the best way to do this. totalResponseArray
is a custom class of type Array<Duplet<Product, imageArray>>
.
let sizeDict = totalResponseArray.flatMap { (obj) -> [String:String] in
var storedItem = [String:String]()
storedItem["\(obj.one.productID)"] = "\(obj.one.size)"
return storedItem
}
The other way I can think to do this is using a loop but I feel like there is a cleaner way to do it.
I am trying to apply a flatmap function to an array not a dictionary. The result of the flatmap function should be a dictionary of [String:String]
.
Here is the result I am trying to get. Is there a better or cleaner way of doing it? I figured out that the above code doesn't make sense to get this same result as the code below.
var storedItem = [String:String]()
for eachItem in totalResponseArray{
storedItem["\(eachItem.one.productID)"] = "\(eachItem.one.color)"
}
EDIT: It turns out that the loop is the best way and only way to do it. I am not trying to map the function.