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

Unable to pass challenge task in error handling on swift

Not sure why this isn't an accepted answer:

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 case let someValue?? = data["someKey"] else {
            throw ParserError.invalidKey
        }
    }
}

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

do {
    try parser.parse()
} catch ParserError.invalidKey {
    print("Got invalid key")
} catch ParserError.emptyDictionary {
    print("Got an empty dictionary")
}
error.swift
enum ParserError: Error {
  case emptyDictionary
  case invalidKey
  case InvalidKey
}

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

  func parse() throws {
    func parse() throws {
        guard let data = data else {
            throw ParserError.emptyDictionary
        }

        let keyExists = data["someKey"] != nil

        if let value {
          throw ParserError.InvalidKey
        }
    }
  }
}

let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)
Max Ramirez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Max Ramirez
iOS Development Techdegree Graduate 17,361 Points

Hey Shahrier, In the challenge it says "Hint: To get a list of keys in a dictionary you can use the keys property which returns an array. Use the contains method on it to check if the value exists in the array" Your top portion for emptyDictionary is correct. Lets work on the invalidKey. Following the hint: keys property & use the contains method to check if the value exists in the array.

guard data.keys.contains("someKey") else {
            throw ParserError.invalidKey
        }

We get keys & contains using dot notation on data

Then when we do the do catch statement for task 2:

do {
try parser.parse() // Trys the method from Parser struct on the instance of Parser
} catch { // This is a generic catch block
print("Error: \(error)")
}

And using pattern matching We do the following using our ParserError Enum for task 3

do {
 try parser.parse() // same as before
} catch ParserError.emptyDictionary { // All we are doing is adding the parser error enum member "emptyDictionary"
print("Error! Empty Dictionary") // own message, yours can be whatever you want
} catch ParserError.invalidKey {
print("Error! Invalid Key") // again own messege
}

I hope this helped!

3 Answers

Gotcha. Thanks Max!

Zach Hudson
Zach Hudson
7,702 Points

How does this code make sure the dictionary is not empty?

guard let data = data else {
            throw ParserError.emptyDictionary
        }