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 Enumerations and Optionals in Swift Introduction to Optionals Early Exits Using Guard

Anthia Tillbury
Anthia Tillbury
3,388 Points

Using guard statement on nested dictionary?

I wanted to use the Guard statement on a nested dictionary but it will not return the key value:

let weatherDictionary: [String : [String : String]] = [
    "currently" : ["temperature": "22.3"],
        "daily":  ["temperature": "22.3"],
        "weekly":  ["temperature": "22.7"]
]


func dayHighTemp(weatherDictionary: [String : [String : String]]) {
    guard let dailyWeather2 = weatherDictionary["weekly"], let highTemperature2 = dailyWeather2["temperature"] else {
        return
    }
    print(highTemperature2)
}

The code compiles but it will not return a value. I tried adding one more check for the "22.7" pair in the Dictionary but the compiler threw a fit.

I know that Dictionaries are Optional Types and that later on I will be dealing with them a lot more in code when pulling data from a server, so rather than follow the example Passan showed I wanted to see how it would work on a nested Dictionary.

4 Answers

Tassia Castro
seal-mask
.a{fill-rule:evenodd;}techdegree
Tassia Castro
iOS Development Techdegree Student 9,170 Points

Hey Anthia Tillbury,

Your code looks great. If I understood correctly, you just need to call the method func dayHighTemp(weatherDictionary: [String : [String : String]]) and pass the constant weatherDictionary as an argument to see the value of the key highTemperature2 being printed.

Hope it helps,

Tassia Castro
seal-mask
.a{fill-rule:evenodd;}techdegree
Tassia Castro
iOS Development Techdegree Student 9,170 Points

Your code to unwrap the dictionary and later print its value is inside a function called dayHighTemp(). Independently of what you're doing, that code (inside the function) will only be executed if you call the function.

print() is also a func. If you had called it outside dayHighTemp you would get the same result.

print(weatherDictionary["weekly"]!["temperature"]!)

But, as you called print() from inside another function, dayHighTemp(), then print() will only print something if dayHighTemp() is called.

Anthia Tillbury
Anthia Tillbury
3,388 Points

I see, it was correct, but I'm unsure as to why I needed to call the function? I suppose I would have done that if this were normal code and not an example of unwrapping an Optional (Dictionary) anyway.

Many thanks!

Anthia Tillbury
Anthia Tillbury
3,388 Points

Oh no, The Bang Operator, I'm told never to do that!

Yes, I forgot that it was inside a function and therefore needed to be called to bring it to life. Other times just an if let statement which didn't need to be called.

I should have known better, many thanks!