Programming in Swift · Challenge: Dictionaries | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5994-programming-in-swift/lessons/32

Is there a hidden danger or problem with using nil coalescing on the last challenge instead of an if let statement or a guard statement?

Here is my code:
func printInfo(info: [String: String]) {
print(info[“City”] ?? “No City.”, info[“State”] ?? “No State.”)
}

Not that I am aware of! In this example, it’s just a matter of clarity for humans reading your code, and deciding what best expresses your intent.

I am not very smart - can you tell me why Jessy defined the parameter in the function as “ofPerson person”? is that just for clarity?

Thank you!

Hi! Please don’t be down on yourself for not knowing this.

Adding a preposition like “of” to the argument label (“ofPerson”), but not the parameter name (“person”), is just a naming convention. It’s meant to make the method call read more like a sentence (“Print location of person, Catie”), but in my opinion this is one of the more difficult conventions to clearly get “right”.

You can reach a bit about it in the Swift API Design Guidelines. If you’re interested, you can also probably find a variety of opinions on the matter with your search engine of choice!