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

Michael Reining
Michael Reining
10,101 Points

Stuck with Part 1 of Challenge

Not sure what I am doing wrong in this code challenge.

Can someone please take a look at my code and let me know why it is not letting me pass the first step of the challenge?

enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

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

    func parse() throws {
        if data == nil { // Part 1 - if data is nil throw empty dictionary error
            ParserError.EmptyDictionary
        } else {
            // Part 2 - if someKey does not exist, throw invalid key error
            if ((data!.keys.contains("someKey")) == false) { 
                ParserError.InvalidKey
            }
        }

    }
}

let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)
error.swift
enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

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

    func parse() throws {
        if data == nil {
            ParserError.EmptyDictionary
        } else {

            if ((data!.keys.contains("someKey")) == false) {
                ParserError.InvalidKey
            }
        }

    }
}

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

2 Answers

macserwicki
macserwicki
8,072 Points

Here is the answer for Part 2. You're only missing the do{} part.

enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

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

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

        guard let key = data["somekey"] else {
            throw ParserError.InvalidKey
        }

    }
}

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

Part 3 Answer Below. Try it on your own before copying :) Just add your 2 error types to the catch{}.

//

//

//

//

//

//

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

I've stuck on part 2 of this challenge.

My code for first task passed but returned errors in second part.

enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

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

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

        guard let key = data["somekey"] else {
            throw ParserError.InvalidKey
        }

    }
}

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

Someone help us!

Evans Attafuah
Evans Attafuah
18,277 Points

this is my answer and it worked

enum ParserError: ErrorType {
  case EmptyDictionary
  case InvalidKey
}

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

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

    guard let _ = data!["somekey"] else {
      throw ParserError.InvalidKey
    }

  }
}

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

do {
  try parser.parse()
} catch ParserError.EmptyDictionary {
  print("empty dictionary")
} catch ParserError.InvalidKey {
  print("missing key")
}