Using multiple values in a dictionary

For my app, I need to simply take a key (that has multiple values attached to it) from a dictionary and pick a random one of its values to assign to a variable. I think the way to find the number of values of a key and pick a random value would be easy but assigning that value to a variable I don’t know how to do yet. Here is what I’m thinking of doing for the random value picker:


let dict: [String: [String]] = [
“Numbers“: [“One”, “Two”, “Three”],
“Letters”: [“A”, “B”, “C”]
]

var count = 0

dictKey = "Letters"

//Finding how many values are in the key "Letters"
for value in dict[dictKey] {

count = count + 1

}

print(count)
//This should be how many values there are in the "Letters" key

var randomValueNum = arc4Random_uniform(count - 1)


I have never used for-in loops before so correct me if I used it incorrectly there, and if there is an easier way to do the above code, please tell me.


Now after that code, I need to extract the string from the value at [randomValueNum] (e.g. If randomValueNum was 0, the value would be "A", if it was 1, the value would be "B", etc.). Once I have that string, I need it in a varible as a String. Thanks in advance.

Answered by QuinceyMorris in 245128022

The values in the dictionary are arrays, so you don't need to count their contents manually:


     let count = dict [dictKey]!.count


Your random number number is not quite right, because the parameter to arc4random_uniform is an upper bound, not a maximum value. The returned value is always less than the number you pass in. So:


let index = arc4random_uniform (count)


(Otherwise you'll never use the last element of the array.) Then you just need to go back an index into the array:


let randomlyChosenValue = dict [dictKey]! [index]


Note that looking up a dictionary returns an optional value, which is why you need to unwrap ("!") the results. If you want, you could do this once and assign the resulting (non-optional) array value to a local variable.

Accepted Answer

The values in the dictionary are arrays, so you don't need to count their contents manually:


     let count = dict [dictKey]!.count


Your random number number is not quite right, because the parameter to arc4random_uniform is an upper bound, not a maximum value. The returned value is always less than the number you pass in. So:


let index = arc4random_uniform (count)


(Otherwise you'll never use the last element of the array.) Then you just need to go back an index into the array:


let randomlyChosenValue = dict [dictKey]! [index]


Note that looking up a dictionary returns an optional value, which is why you need to unwrap ("!") the results. If you want, you could do this once and assign the resulting (non-optional) array value to a local variable.

Ok thanks! All that makes sense. I didn't realize values in dictionaries were arrays. That makes it so much easier.

Not in general, but you have this:


     let dict: [String: [String]]


The outer [ … : … ] construct is the syntax that means a dictionary. The inner [ … ] (following the ":") means an array, of String values in this case. This:


     let dict: [String: Int]


would mean a dictionary whose values are plain ol' Int values.

Using multiple values in a dictionary
 
 
Q