iOS Error Handling in Swift Error Handling Handling Errors

connor hoare
connor hoare
iOS Development Techdegree Student 7,923 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
iOS Development with Swift Treehouse Moderator 43,640 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,272 Points

Hi! here is the solution to the challenge:

[Moderator redacted]
Michael Hulet
Michael Hulet
iOS Development with Swift Treehouse Moderator 43,640 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!