In a pair of data, how do I determine which one is the key and which one is the value?
For instance, if I wanted to make a list of books and authors, do I put the author or the book as the key—and why?
Thanks!
It’s usually contextual. The value
very often tells you something about the key, but with a value alone, it’s worthless.
Imagine the number 14, then 29, then 45…
…
They mean nothing. But if you set them up as prices for three items: Book1, Book2TheSequel, Book3TheEpicFinale, it makes sense to match those values to their corresponding book.
However, you would rarely see a bookstore organize their books by price. (This is a very loose analogy, since online bookstores you can definitely filter things by price first if you really wanted to).
Also, a key may have a value that is an array of values. So one book title can hold much information (genre, author, price).
When you phrase it like that, it’s very helpful! Thank you so much for going further into detail. I have a better understanding now, and will keep this in mind when doing more practice. Thanks again!
Can someone tell me why we use an equal sign in the first var and colon in the second
var episode = 11
var villain: String
switch episode…
The one with the value stores an actual value in memory.
The one with the type declaration basically says: “reserve a space in memory that’s going to take a String
, what the String
is will be specified later”.