Home > Enterprise >  Crate dictionary with for in in swift
Crate dictionary with for in in swift

Time:10-06

I'm trying to make a dictionary with looping using for in swift

but the result that appears is only the last value

my code is

  let items = [10, 9, 8 , 7, 6]
    
  var paramp: [String: Any] = [:]
  for item in items {
        paramp["paramp_id"] = item
  }

the resulst is ["paramp_id" : 6]

the results i want is

["paramp_id" : 10]
["paramp_id" : 9]
["paramp_id" : 8]
["paramp_id" : 7]
["paramp_id" : 6]

i want to get all the dictionary

CodePudding user response:

You seem to be describing an array of single-value dictionaries. That's unlikely to be useful. What you really want is probably an array of structs that have a paramp_id property:

struct MyStruct {
    let paramp_id: Int
}
let items = [10, 9, 8, 7, 6]
let structs = items.map {MyStruct(paramp_id:$0)}
  • Related