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

Is it me or is the unit test? Using `keys.contains`

I'm trying to determine whether my code isn't working the way I expect it to and my understanding is flawed or the unit test/compiler is too inflexible to account for my solution.

I wanted to try to solve this challenge using the keys.contains method rather than data["someKey"] != nil since that's what the directions suggest.

Here's the code I wrote:

enum ParserError: Error {
    case emptyDictionary
    case invalidKey
}

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

    func parse(data dict: [String : String?]) throws {
        guard let data = data else {
            throw ParserError.emptyDictionary
        }

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

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

// NOTE: I'm not including the following in the code challenge, just checking it in Xcode to validate the same output with valid data.
let validData: [String : String?]? = ["someKey": "I'm valid"]
let validParser = Parser(data: validData)

This returns the following if I fully expand it:

["someKey": nil]
    (key "someKey", nil)
        key "someKey"
        nil
["someKey": "I'm valid"]
    (key "someKey", value "I'm valid")
        key "someKey"
        value "I'm valid"

Unfortunately, I get a compiler error from the Treehouse compiler and when I try to preview it it shakes and gives me no feedback.

After searching around I found this in one of the answers:

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
        }
    }
}

This returns:

["someKey": nil]
    (key "someKey", nil)
        key "someKey"
        nil
["someKey": "I'm valid"]
    (key "someKey", value "I'm valid")
        key "someKey"
        value "I'm valid"

Unless I'm missing something, the output of the function is identical and presumably should pass Treehouse's unit tests — especially since my code is closer to the style @pasan seems to favor in the course — so I'm surprised I'm not able to pass the challenge.

I have to admit, I'm still having a hard time fully wrapping my head around optionals.

Ah, it definitely is different because with my method I have to pass it data and unwrap it but in the second solution I just call the method

Mine:

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

do {
    try parser.parse(data: data!)
} catch {
    print("Invalid")
}

vs

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

do {
    try parser.parse()
} catch {
    print("Invalid")
}

However, I don't understand why, is this a scope problem?