swift - Error: Immutable value passed on reduce function -
i'm trying following code transforms array of tuples dictionary i'm receiving compile error saying:
immutable value of type '[string : string]' has mutating members named 'updatevalue'
var array = [("key0", "value0"), ("key1", "value1")] var initial = [string: string]() var final = array.reduce(initial) { (dictionary, tuple) in dictionary.updatevalue(tuple.0, forkey: tuple.1) return dictionary }
why if initial declared var? have @noescape on reduce's signature?
func reduce<u>(initial: u, combine: @noescape (u, t) -> u) -> u
you can make dictionary
parameter mutable preceding var
:
var final = array.reduce(initial) { (var dictionary, tuple) in ^^^
note using reduce
new dictionary created @ each iteration, making algorithm inefficient. might want consider using traditional foreach
loop
Comments
Post a Comment