Adrian Adrian - 1 month ago 3x
Swift Question

Search Array of Dictionaries for Value in Swift

I'm new to Swift and taking a class to learn iOS programming. I find myself stumped on how to search in an array of dictionaries for a string value and dump the string value into an array. This is taken from my Xcode playground.

I'm trying to figure out how to:
1) search through an array of dictionaries
2) dump the results of the search into an array (which I've created)

These are the character dictionaries.

let worf = [
"name": "Worf",
"rank": "lieutenant",
"information": "son of Mogh, slayer of Gowron",
"favorite drink": "prune juice",
"quote" : "Today is a good day to die."]

let picard = [
"name": "Jean-Luc Picard",
"rank": "captain",
"information": "Captain of the USS Enterprise",
"favorite drink": "tea, Earl Grey, hot"]

This is an array of the character dictionaries listed above.

let characters = [worf, picard]

This is the function I'm trying to write.

func favoriteDrinksArrayForCharacters(characters:Array<Dictionary<String, String>>) -> Array<String> {
// create an array of Strings to dump in favorite drink strings
var favoriteDrinkArray = [String]()

for character in characters {
// look up favorite drink

// add favorite drink to favoriteDrinkArray

return favoriteDrinkArray

let favoriteDrinks = favoriteDrinksArrayForCharacters(characters)


I would be grateful for any assistance on how to move forward on this. I've dug around for examples, but I'm coming up short finding one that's applicable to what I'm trying to do here.


Inside the loop, you need to fetch the "favorite drink" entry from the dictionary, and append it to the array:

for character in characters {
    if let drink = character["favorite drink"] {

Note, the if let drink = guards against the possibility there is no such entry in the array – if there isn't, you get a nil back, and the if is checking for that, only adding the entry if it's not nil.

You might sometimes see people skip the if let part and instead just write let drink = character["favorite drink"]!, with an exclamation mark on the end. Do not do this. This is known as "force unwrapping" an optional, and if there is ever not a valid value returned from the dictionary, your program will crash.

The behavior with the first example is, if there is no drink you don't add it to the array. But this might not be what you want since you may be expecting a 1-to-1 correspondence between entries in the character array and entries in the drinks array.

If that's the case, and you perhaps want an empty string, you could do this instead:

func favoriteDrinksArrayForCharacters(characters: [[String:String]]) -> [String] {
    return { character in
        character["favorite drink"] ?? ""

The .map means: run through every entry in characters, and put the result of running this expression in a new array (which you then return).

The ?? means: if you get back a nil from the left-hand side, replace it with the value on the right-hand side.