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

Jeff Ripke
Jeff Ripke
41,989 Points

In the editor, you have a struct named Parser whose job is to deconstruct information from the web. The incoming data ca

I am having a hard time figuring this question out. Not sure what to do next.

error.swift
enum ParserError: ErrorType {
  case EmptyDictionary
  case InvalidKey
}

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

  func parse() throws {
    guard let data = 
  }
}

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

4 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Firstly, we initialize a new instance of parser with a dictionary. Once initialized, we can call the function parse to parse the data.

The function parse will throw an error in one of the following cases:

  1. the dictionary data, an optional, is nil
  2. data does not contain the key "someKey"

The code would be something like this.

func parse() throws {

    // 1. Make sure data is not nil.
    // If it is nil throw an error, otherwise
    // unwrap it to var parsedData
    guard let parseData = data else {
        // This will throw and exit the parse
        // function right away
        throw ParserError.EmptyDictionary
    }

    // 2. Make sure parseData contains key "someKey"
    guard parseData.keys.contains("someKey") else {
        throw ParserError.InvalidKey
    }

    // If no errors are thrown, proceed here...
}

Hope that helps :)

macserwicki
macserwicki
8,072 Points

You're only missing the "try" keyword on the "parse()" function. You need it because the "parse()" function "throws".

enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

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

    func parse() throws {
        guard let parsedData = data else {
            throw ParserError.EmptyDictionary
        }
        guard parsedData.keys.contains("someKey") else {
            throw ParserError.InvalidKey
        }
    }
}

let data: [String : String?]? = ["someKey": nil]


do {
    let parser = try Parser(data: data)
   try parser.parse()
} catch {

}
Jeff Ripke
Jeff Ripke
41,989 Points

Thanks for helping that worked. Know I am having a hard time figuring out the second part:

error.swift
enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

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

    func parse() throws {
        guard let parsedData = data else {
            throw ParserError.EmptyDictionary
        }
        guard parsedData.keys.contains("someKey") else {
            throw ParserError.InvalidKey
        }
    }
}

let data: [String : String?]? = ["someKey": nil]


do {
    let parser = try Parser(data: data)
    parser.parse()
} catch ParserError.EmptyDictionary {

}

Thanks,

Jeff

my code kept failing until i used dot notation

""" let data: [String : String?]? = ["someKey": nil] do { let parser = try Parser(data: data).parse() } catch let error { print("error") } """