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 trialDevin Boddie
6,777 PointsSwift 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
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
kols
27,007 PointsNathalie 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
5,367 PointsLooks 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
Full Stack JavaScript Techdegree Student 14,045 PointsI 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.