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

Pratham Mishra
Pratham Mishra
13,154 Points

Can someone tell me what's wrong with my code, as I am getting an error:- Your code could not be compiled

I'm not able to understand the reason behind the error!

error.swift
enum ParserError: Error {
    case emptyDictonary
    case invalidKey
}
struct Parser {
    var data: [String : String?]?

    func parse() throws {
        guard let parseData = data else {
            throw ParserError.emptyDictonary
        }
        guard parseData.keys.contains("someKey") else {
            throw ParserError.invalidKey
        }
    }
}
let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data : data)

1 Answer

Steven Parker
Steven Parker
210,131 Points

If you use the "preview" button, it will show you the messages from the complier. In this case:

swift_lint.swift:13:19: error: type 'ParserError' has no member 'emptyDictonary'
            throw ParserError.emptyDictonary
                  ^~~~~~~~~~~ ~~~~~~~~~~~~~~
swift_lint.swift:4:8: note: did you mean 'emptyDictionary'?
  case emptyDictionary
       ^

So the issue is the speilling of "emptyDictonary" instead of "emptyDictionary".