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 trial

iOS Swift 2.0 Collections and Control Flow Dictionaries Modifying a Dictionary

Mert Kahraman
Mert Kahraman
12,118 Points

How do we change the existing keys without changing their values?

We saw how to change the values for certain keys, but is there a way to directly change the key of a value as well? For example, can we change the key value pair "LGA" : "La Guardia" to "ABC" : "La Guardia" directly? Or do we need to delete the LGA one and add the ABC one?

2 Answers

Michael Hulet
Michael Hulet
47,913 Points

A dictionary maintains a relationship between a key and a value, but not between a value and a key. In other words, while there may be a valueForKey: method, there's no keyForValue: method, because keys can't be referenced by their value. Thus, the only way that's possible is the second method you described

Michael Hulet
Michael Hulet
47,913 Points

FYI: Your answer is in Python, but the question is in Swift