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

Pratham Mishra
Pratham Mishra
14,191 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
231,268 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".