Reputation: 740
I tried to append different values for same key in dict but getting error please help to solve this.. both below dictionary initializations giving errors.
//one way of declaring dict
public var List = Dictionary<String, Array<Any>>()
// second way of declaring dict
public var List = [String: [String]]()
let bDDR = "b4474rb74g"
let pressure = 20
List[bDDR].append(pressure)
//error is: Cannot subscript a value of type '[String : [String]]' with an index of type '[String]'
let voltageValue = 3.90
let tempValue = 97
thanks in advance...
Upvotes: 2
Views: 1848
Reputation: 100533
You can try this if you wan to overwrite old values
List.updateValue([pressure],forKey:bDDR)
if you want to append
List.updateValue(List[bDDR]! + [pressure],forKey:bDDR)
Upvotes: 0
Reputation: 2477
You cannot append it in place. Because List[BDDR]
gives you a constant copy of the array. You have to assign the list array to a variable, append the value and then set ti back to the dict.
var list = List[BDDR]
list?.append(pressure)
List[bDDR] = list
Also, public var List = [String: [String]]()
, will not work, because you are appending an Int
instead of a String. public var List = Dictionary<String, Array<Any>>()
will work because Int is also Any.
Upvotes: 1