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 Error Handling Handling Errors

connor hoare
connor hoare
7,932 Points

I literally am confused y the question and don't have a clue what to do here.

Please assist me what steps I should take and what to change the catch statements to

error.swift
enum ParserError: Error {
    case emptyDictionary
    case invalidKey
}

struct Parser {
    var data: [String : String?]?

    func parse() throws {

        guard let data = data else {
            throw ParserError.emptyDictionary
        }

        guard data["somekey"] != nil else {
            throw ParserError.invalidKey


        }
    }
}

let data: [String : String?]? = ["someKey": nil]

do {
    let parser = Parser(data: data)
    try parser.parse()
} catch ParserError.emptyDictionary {
} catch ParserError.invalidKey {
}
Michael Hulet
Michael Hulet
Treehouse Moderator 47,822 Points

Your code as posted passes all 3 steps of the challenge when I paste it in. What is your question, exactly?

1 Answer

tromben98
tromben98
13,273 Points

Hi! here is the solution to the challenge:

[Moderator redacted]
Michael Hulet
Michael Hulet
Treehouse Moderator 47,822 Points

Hey tromben98!

For future reference, it's frowned upon in the Community to post code that can be copied and pasted and just work without any explanation of why it works and why it's the correct answer. As such, I've redacted your code here. If you'd like, feel free to re-post your solution, but be sure to give a thorough explanation of why it works and why it's the correct answer. Thanks for helping out in the Community!