Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Enumerations and Optionals in Swift Introduction to Optionals Downsides to Using If Let

Lana Wong
Lana Wong
3,968 Points

Can you use dictionary keys when you have not created a value?

Hi, Pasan created a dictionary key while unwrapping called, "name". I'm a little confused, Can you use dictionary keys when you have not created a value? And that key didn't exists, so can you just create the key on the spot?

2 Answers

Xavier D
PLUS
Xavier D
Courses Plus Student 5,840 Points

Hi Lana, I think you can have an empty key or value when you have a dictionary of strings like below

let airportCodes = ["CDG": "Charles De Gaulle", "MTS": "", "": "Empty Key String"]

let newYorkAirport = airportCodes["JFK"]

print(airportCodes)

But when I tried duplicate this with an Int to Int Dict like below, I got an error...

let intToIntDict = [1:100, 2:200, 3:300, 4 : , : 500]

I think you can get away with the emptiness with strings because of the syntax...denoting the quotation marks with nothing in-between shows that there's a string but it's empty. But with Ints, I don't think there's syntax to show an empty key or value in a dictionary...I'm not sure why there would be a need for an empty key or value

...I do know that with dictionaries....sometimes you may not always know what's in a particular dictionary that you're working on...and you can search the dictionary using the keys... and if you enter a key that doesn't exist in a dictionary... you get back nil and the dictionary doesn't crash for giving you nil

The reason why you get away with the emptiness in a string is because

let stringValue = ""

is the same as

let stringValue = String()

You can't have a empty Int though, a Int is a number, can't have a invisible or empty number(well in the dictionary you are creating with literals that is). You call or get back nil from your dictionary the app will fatal crash for unexpectedly trying until wrap nil, unless you have some error handling around this use case for getting nil. In Objective-C I think it would pass over the nil and not crash, but Swift being a safe language and all, it has a issue with passing around nil values, it causes unexpected behavior.

To answer the original question of this post, yes you can make up your keys on spot, as long as they are created before you actually try to access them then you are fine.