Reputation: 83
i have a key value pair that contains key string and [issueFieldModal] array so i have a object element which has e tab name and elementArray element has a key and value property
var keyValuePair = KeyValuePairs<String, [ISSUEFieldModal]>()
var issueFieldArray = [ISSUEFieldModal]()
for element in elements {
for newElement in element{
let issueField = ISSUEFieldModal(newElement.key, newElement.value)
issueFieldArray.append(issueField)
}
keyValuePair = [element.tabName: issueFieldArray]
}
but when i print it out the key value pair only the last appending shows up
i think it override the old values and set in every loop
thank you
Upvotes: 7
Views: 4461
Reputation: 236420
The issue there is that KeyValuePairs
unlike a regular dictionary, doesn't conform to Hashable
. You cannot assign through subscript. You may have duplicated key/value pairs. It conforms to RandomAccessCollection
so through subscript you can only get but not set a value.
keyValuePair = [element.tabName: issueFieldArray]
works because KeyValuePairs
conform to ExpressibleByDictionaryLiteral
but you can NOT append a new key value pair because it doesn't conform to RangeReplaceableCollection
. In other words you need to add all key value pairs when initialising your collection.
Upvotes: 11