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 Enumerations and Optionals Introduction to Optionals Downsides to Using If Let

Nicholai Hansen
Nicholai Hansen
2,395 Points

What are the inputs in createFriend supposed to signify?

If you call this function createFriend, what are the inputs supposed to signify?

For example, if I create a constant named friend, and call the function -> let friend = createFriend([String: String]) ... Are the two strings supposed to be the name and age of the friend? Where is the third input for the address? No matter what strings I enter for these inputs, my results are nil. Even if I enter -> let friend = createFriend(["name": "age"])... any idea?

1 Answer

Keli'i Martin
Keli'i Martin
8,227 Points

So the input into the function is supposed to represent a dictionary that contains key-value pairs for the information necessary to create a friend. For example, ["name" : "John"] would be an example of an entry in one such dictionary.

In practice, let's say that the dictionary we want to pass into the function is declared as follows:

let friendDictionary: [String : String] = [
    ["name" : "John"],
    ["age" : "35"],
    ["address" : "123 Main Street"]
]

Now you can just pass that dictionary into the function and have access to the information inside it.

let friend = createFriend(friendDictionary)
print(friend.name) // prints "John"
print(friend.age) // prints "35"
// etc...