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 trialJeff Ripke
41,989 PointsIn 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.
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
Courses Plus Student 11,071 PointsFirstly, 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:
- the dictionary
data
, an optional, is nil - 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
8,072 PointsYou'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
41,989 PointsThanks for helping that worked. Know I am having a hard time figuring out the second part:
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
MUZ140734 Tinashe Chibwe
16,960 Pointsmy 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") } """