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

Devin Boddie
Devin Boddie
6,777 Points

Swift 3 Error Handling Code Challenge Help

The Challenge Question is below as well as my answer. To be honest i'm struggling to show in code "if the data is nil" in order to throw the Empty Dictionary error. Could anyone help me understand what I'm doing wrong here?

In the editor, you have a struct named Parser whose job is to deconstruct information from the web. The incoming data can be nil, keys may not exist, and values might be nil as well.

In this task, complete the body of the parse function. If the data is nil, throw the EmptyDictionary error. If the key "someKey" doesn't exist throw the InvalidKey error.

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

error.swift
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 let key = data["somekey"] else {
            throw ParserError.invalidKey
}

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

3 Answers

Nathalie Stenhammer is correct; pesky curly braces! :) However, if you want to use the 'contains' method as they suggest in the "Hint" section of this code challenge — (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) — an alternate way to answer this would be:

enum ParserError: Error {
    case emptyDictionary
    case invalidKey
}

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

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

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

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

do {
    try parser.parse()
} catch ParserError.emptyDictionary {
    print("The dictionary is empty.")
} catch ParserError.invalidKey {
    print("The key 'someKey' does not exist.")
}
Nathalie Stenhammer
Nathalie Stenhammer
5,367 Points

Looks like you only miss some curly braces (-:

This is my solution :

 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 let key = data["somekey"] else {
                  throw ParserError.invalidKey
           }
      }
 }

 let data: [String : String?]? = ["someKey": nil]
 let parser = Parser(data: data)
Jesse Gay
seal-mask
.a{fill-rule:evenodd;}techdegree
Jesse Gay
Full Stack JavaScript Techdegree Student 14,045 Points

I was first thrown off by the hint. For the second condition, I was able to pass with:

let myKeys = data.keys
       if myKeys.contains("someKey") {
     } else {
           throw ParserError.invalidKey
      }

but Nathalie's solution seems simpler and more elegant to me.