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
207,980 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".