how to access values in a dictionary using reduce in swift? -


i got key dictionary using reduce doing following:

let namesandscores = ["anna": 2, "brian": 2, "craig": 8, "donna": 6]   let namesstring = namesandscores.reduce("",  combine: { $0 + "\($1.0), " })  print(namesstring) 

but know how value dictionary using reduce ?.

any appreciated. thanks.

i'll suggest easier way

let names = namesandscores.keys.joinwithseparator(", ") //  brian, anna, craig, donna  let values = namesandscores.values.map(string.init).joinwithseparator(", ") // 2, 2, 8, 6 

update: code reduce

 let values = string(namesandscores.values.reduce("") { "\($0), \($1)"}.characters.dropfirst(2)) // 2, 2, 8, 6 

Comments