Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Akrapovic

macrumors 65816
Original poster
Aug 29, 2018
1,193
2,570
Scotland
I'm trying to use the brand new Sift Chart framework, and the chartForegroundStyleScale function takes a KeyValuePairs as the argument. I tried supplying a Dictionary (of [String:Color], but I get the error Cannot convert value of type '[String : Color]' to expected argument type 'KeyValuePairs<DataValue, S>'.

Ok so fair enough, I need to convert the Dictionary to a KeyValuePair. I tried creating the KVP the same way I created the Dictionary, but this doesn't work. I loop through the appropriate array and build a dictionary, but this doesn't work with a KVP as you cannot modify a KVP once it's created. I also need to add around 40 Keys and Pairs to the item, not a single one.

I tried creating a KVP from the dictionary like this

Swift:
let myKVP = KeyValuePairs(dictionaryLiteral: myDictionary)

But this fails with the error Cannot convert value of type '[String : Color]' to expected argument type '(Key, Value)'

All the tutorials I find involve typing out and hard coding the KVP data, which isn't possible for me, as the data will be different each time the app opens. So how do I generate a KVP?
 

hugo7

macrumors regular
Oct 25, 2008
180
94
Hi there, I’ve not tried this but looking at the Apple documentation, a DictionaryLiteral appears to be one or more tuples of (key, value)

init(dictionaryLiteral elements: (Key, Value)...)

You can use map to do the necessary conversion

myDictionary.map { <convert each member to a tuple here> }
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.