iOS Swift Collections and Control Flow Dictionaries in Swift Introduction to Dictionaries

David Duong
David Duong
513 Points

When creating a dictionary can you have long definitions? For instance ["key1" : "paragraph over several sentences"]?

If not could you create a dictionary as ["key1" : (place holder for paragraph)]?

1 Answer

Michael Hulet
MOD
Michael Hulet
Treehouse Moderator 46,831 Points

A dictionary can hold anything, as long as its keys are Hashable. That means you can add any string you'd like, no matter how long. Swift can handle it all!

David Duong
David Duong
513 Points

So you would create a dictionary with ["key1" : "abc", "key2" : "def"] in the viewController then in another Swift file have it set up so that "abc" = """paragraph1""", "def" = """paragraph2"""? Of course you'd have to set up your class and variables, etc.

Much like how an actual dictionary works. There is a word and tapping on that word leads to a long definition or multiple definitions.

Michael Hulet
Michael Hulet
Treehouse Moderator 46,831 Points

That's something you could do, if you wanna. It doesn't matter whether you initialize it that way or if you set it to be that way after you've defined it (assuming you store it as a var instead of a let), it can hold whatever you want it to