Dictionaries

Dictionaries are a powerful tool in creating data-heavy shortcuts. Learn how to work with Shortcuts.

Download Links:

Dictionaries Shortcut

Transcript

This video follows the prior video about Lists. And if you haven't seen that one yet, I recommend you go back and watch it first, because that's kind of the introduction to Dictionaries. So where lists are a group of things you've stuck together that don't necessarily have anything in common. Dictionaries are a little bit more formatted, a little bit organized, a little bit more powerful type of lists.

To see them go into the search bar, and just type Dictionary, and you can see there's the primary tools. And one of the first way is I usually use a dictionary, just drag the dictionary command in, and then you can start adding data to it. So I'm going to tap the little Plus button to add new item. And the first one will be First Name. Next one will be a text field will be Last Name. And then the next will do a Boolean field, which is yes or no, fancy speak for programmers meaning yes or no. And we'll say, Shortcuts User.

Alright, so we've got three fields of data in here. And you can already see some differences between the list. First thing is you've got each a label and a data for each line. Whereas with List, you just have the data itself. Now the nice thing about these labels, as they kind of give us a way to access these variables later in the shortcut without having to identify which line they're on like on a list. It's just a lot more organized and easier to use. For example, let's say you've got some boxes on your shelf. If you got three boxes up there, and there's no labels on them, you know that they hold stuff, you're just not sure what's in them until you pull them off the shelf and start looking through them. And that's what you get with a list. You don't know what kind of data is in there until the program actually access it. With a dictionary, you actually know exactly what's in there, because each box in essence has a label on it. In this case, first name, last name and Shortcuts user, and you know what type of stuff is in that box. In this case, the first two boxes have a text field and the third box has a Boolean field. So that gives us more to work with as we're making our shortcuts. Let's go ahead and add an alert to access some of this data.

Okay, said my name is that I'm going to access the dictionary. So I'm going to tap the Dictionary button next to the Magic Variable. And that just drops it in there. Now if I tap on that, it gets me the option to get specific types of data out of there. And I'm going to get the value used for the key First Name, we call that first column to keys. So we're going to go ahead and type in First Name. And now it's going to insert whatever we've got placed in the data field for first name, so let's add some data. David Sparks Shortcuts user. Oh, yeah. Okay, so we've got data in there, we're going to use that in a script. So I'm going to go ahead and run this. And it gives me alert. My name is David. Great.

Now, one of the things I like about Dictionaries is an addition to the better organization of this data we've got, it also gives us the ability to make changes to specific lines of the dictionary, what you can't do with a list, you have to really recreate the entire list, if you want to change something on it. With a dictionary, we can go in and surgically change one item.

So I'm going to go back up and search for dictionary again. And you can say, here's one set dictionary value, I'm going to add this to the bottom after the alert. And there's three parameters, the key, the value and the specific dictionary. So first thing we're going to do is change the key. The key is the column or the label. So we know the key is, in this case going to be first name, and the value we're going to change it is Omer Samuel, which was almost my name, except for my mom's intervention. And the dictionary we're going to do, we're going to go ahead and select the dictionary with a Magic Variable. And there's the dictionary right there.

Okay, so we've now changed the name. And that first value, that first key has now been changed to Omer Samuel. So let's go ahead and use that with an alert. And we're going to do the same thing, we're going to say my name is. We're going to insert the dictionary, once again, change the dictionary to just that first key, First Name. And if I did everything right here, we should get two alerts with two different names. So let's try it out.

Okay, first, it says My name is David, because that's the first value. Once I press okay, it's going to go to the next step. Change that value to Omer Samuel and then it's going to give us a second alert. Hey, it worked!

So think of dictionaries as boxes with labels. And you can change the labels, and you can change the contents of the box throughout your shortcut. They're a lot more organized, and they're a lot easier to work with when you're working with large shortcuts.

Another reason to use dictionaries is, it lets you set them up right at the beginning of your shortcut, you can just set that dictionaries command like I did, and declare in essence all of your variables at the get go. If later, you want to make changes to the shortcut, you can make changes to those key terms right there, either manually or through the program. Now I did it manually in this case where I reset that key value of the person's name to a different name. But you could actually take a value from a user input or some calculation you received from some portion of your shortcut. And you could go back and change those key values just like I did. But using that variable name.

At this point, we've spent some time going through both Lists and Dictionaries. And hopefully you understand there's uses for both of them. Simple lists are easy and fast. If you've got a real simple shortcut, choose from list is probably the most commonly I implemented because I like the organization, as I explained in the last screencast, but then if I need something more complex dictionaries are there.

You shouldn't let dictionaries intimidate you. They're just a better form of list really, and and don't be afraid to give them a try even when they are overkill. Getting comfortable with the tool, makes it a lot easier to use it more in the future.

Complete and Continue