Iteration 3: Randomize Messages

Iteration 3: Randomize Messages

Iteration Goals

By the end of this iteration, your app should:

  • Provide a random message in response to the emoji the user taps
  • Be complete!

Custom Messages based on Emoji

Before we can generate random messages, we need to have a list of messages that are associated with each emoji/emotion. Write another property in your ViewController.swift class called customMessages that holds a dictionary. This dictionary should have keys of Strings, which hold the name of each emotion, matching the emotions in your emojis property. The values should be Arrays of Strings, each string holding a different message. Here’s an example of one key/value pair:

"ugly crying": ["cheer up buttercup", "take a deep breathe", "do you need to talk?"]

In your alertController, instead of some random string for the message, we are going to have dig into the customMessages dictionary and get info from it. Let’s pull this out into a variable though, because it involved a lot of unwrapping.

Above your alertController, let’s create a new constant:

let emojiMessage = // here I want to extract the word that goes with my emoji
                  // then find that word in the dictionary
                  // get it's value (array)
                  // and access the first element (for now)

Let’s follow the pseudo-code above and take it step-by-step.

First, create a constant which will hold on to the information we access. In it, let’s make sure we can access the word in the emojis dictionary that is associated with our button tapped.

let selectedEmotion = (sender.titleLabel?.text)!
let options = emojis[selectedEmotion]!
// => options should be the String of "ugly crying" if the "😭" button was tapped
// => we had to force unwrap this to promise that `selectedEmotion` help a value

This code should return a string of “ugly crying” if the “😭” was tapped. Now that we have that string, let’s update options to use that string to dig into the customMessages property on the class:

let options = customMessages[emojis[selectedEmotion]!]!
// => options should now be an array, the Array of Strings
//    associated with "ugly crying" in the customMessages dictionary

This returns the array of messages associated with “ugly crying”. The last step is to just access one of those (for now).

let options = customMessages[emojis[selectedEmotion]!]!
let emojiMessage = options[0]

Now that we have this string, let’s change the hard-coded String we had in the alertController for message, to the value of emojiMessage.

Re-build the project in the simulator. You should see the same text appear every time you click each button. Specifically, you should see the string that is the first element in the associated array appear.

Random Messages based on Emoji

Now, instead of hard-coding for the first element in the array of messages for a given emotion, we need to generate a random number.

Go back to our Arrays lesson or read this guide on how to generate a random number. Store a random number in a variable, then use that instead of [0] to access an element from the array.

Re-build in simulator and click each emoji several times - are you now getting a different message occasionally? (The more message options you have, the easier it will be to tell if this is working.)

Challenge

If you have 4 messages in each array and used something like 1 .. 4 in the step above, consider this: what would you do if you had arrays of different lengths? One has 3 elements and one has 10 elements - what would you do?

Commit Your Changes

In your terminal, make sure you are still in the emojional directory. Add and commit your changes. Your commit message should be something like “Complete Iteration 3”.

Push your code up to GitHub so you can come back to it or show off later!