Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialArcee Palabrica
8,100 PointsWhat's a "key"?
Hey guys, Quick question. What's the role of the key "name" in this code?
intent.putExtra("name", name);
Thanks so much :)
2 Answers
Steve Hunter
57,712 PointsHi there,
Data is often held in unordered collections in pairs. A key and a value. The data you want is the value which is accessed by passing the corresponding key to the collection.
The data can be anything, from a simple string to large chunks of data. The key is usually a simple representation of what it is accessing.
So, Intents can have multiple Extras. They are each made up of a key & a value. You access the piece of extra you want by passing the key in; the data you want is then returned. Unlike an ordered collection, e.g. an array where you can access the first, second, 50th element, unordered collections, e.g. dictionaries, are accessed by passing keys to unlock the value.
I hope that helps!
Steve.
Arcee Palabrica
8,100 PointsSteve Hunter awesome! thanks again man... have a great day! :)
Arcee Palabrica
8,100 PointsArcee Palabrica
8,100 PointsSteve Hunter Alright thanks man! that explains it well... So when you say "a simple representation" of what it's accessing you mean you can name it whatever you want right?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsAbsolutely, yes. You can call it whatever you want. Here's a dictionary in the Swift language:
var iceCream = ["CC":"Chocolate Chip", "AP": "Apple Pie", "PB": "PeanutButter"]
Python would be similar:
values = {"name": "Michelangelo", "food": "PIZZA"}
Ruby would look like:
stuff = {'name' => 'Zed', 'age' => 39, 'height' => 6 * 12 + 2}