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 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
Michael Hulet
47,912 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
47,912 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