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 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,842 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,842 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