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

Nikita Voloboev
Nikita Voloboev
4,816 Points

I am really confused about this exercise, I kind of understand what is being asked but not sure about implementation.

I am not sure what I need to do in the guard let statements in the function body and if I even need to pass the dict parameter into the function as the problem description does not say so.

I would appreciate any help. Thank you. :)

error.swift
enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

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

    func parse(dict: [String : String]) throws {
        guard let data == nil else {
            throw ParserError.EmptyDictionary
        }
    }
}

let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)

1 Answer

Nikita, try this:

    func parse() throws {
        if data == nil {
            throw ParserError.EmptyDictionary
        }

        if data!["someKey"] == nil {
            throw ParserError.InvalidKey
        }
    }

Note that the parse() function doesn't take any parameters.