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 Error Handling in Swift 2.0 Error Handling Handling Errors

Van Sanders
Van Sanders
16,087 Points

Not sure I understand what is being asked of me.

This challenge has be stumped. I'm affraid that the way the question is framed doesn't make it clear to me what specifically I'm tasked with. Of course it makes sense to me abstractly (that I'm to return nil if the data doesn't exist and the invalidKey error if there is no such key), but I wonder if anyone could rephrase the question or nudge me?

I'm a bit embarassed that this has had me at a stand-still for 2 weeks. I think its become the type of thig where a word starts to sound weird if you say it too many times to yourself. I'm thinking too hard about this...

1 Answer

Grant Jason
Grant Jason
9,569 Points

Hey Van,

The wording can be a little confusing at times, but don't worry it'll make sense with practice.

Your parse function should contain two guard let statements. They need to be separate because they will be throwing two different errors. In the first statement, you need to check if the the dictionary even exists, and in this case the dictionary is called "data"

The second statement should be checking for valid keys, which we saw in past videos. In this case, your dictionary is data, so you're checking for data["someKey"] and if that doesn't exist, throw the second error.

All in all, your parse function should look something like this:

func parse() throws {
    guard let data = data else {
      throw ParserError.EmptyDictionary
    }
    guard let key = data["someKey"] else {
      throw ParserError.InvalidKey
    }
}

Happy Coding!

Van Sanders
Van Sanders
16,087 Points

Thanks very very much! I'll try not to actually read the code sample and use your instructions first!

You're awesome!