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

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
        }