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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -