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

Angus Muller
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Angus Muller
iOS Development Techdegree Graduate 12,473 Points

Code passed but does not do what expected

Hi, in error handling course for in the Handling Error task I seemed to have managed to get my code to pass the test but in Xcode playground I cannot get it to output the way I think it should. In the below code my believe is that it should print "The dictionary was empty" as the value in data is nil. But in my case it does not print anything. Any clues why?

enum ParserError: Error { case emptyDictionary case invalidKey }

struct Parser { var data: [String : String?]? //think remove ? at end

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

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

    //wrie in here happy code
}

}

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

do { try parser.parse() } catch ParserError.emptyDictionary(let emptyDic) { print("The dictionary was empty") } catch ParserError.invalidKey(let invalidKey) { print("the dictionary did not contain the specified key") }