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 trialMichael Reining
10,101 PointsStuck with Part 1 of Challenge
Not sure what I am doing wrong in this code challenge.
Can someone please take a look at my code and let me know why it is not letting me pass the first step of the challenge?
enum ParserError: ErrorType {
case EmptyDictionary
case InvalidKey
}
struct Parser {
var data: [String : String?]?
func parse() throws {
if data == nil { // Part 1 - if data is nil throw empty dictionary error
ParserError.EmptyDictionary
} else {
// Part 2 - if someKey does not exist, throw invalid key error
if ((data!.keys.contains("someKey")) == false) {
ParserError.InvalidKey
}
}
}
}
let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)
enum ParserError: ErrorType {
case EmptyDictionary
case InvalidKey
}
struct Parser {
var data: [String : String?]?
func parse() throws {
if data == nil {
ParserError.EmptyDictionary
} else {
if ((data!.keys.contains("someKey")) == false) {
ParserError.InvalidKey
}
}
}
}
let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)
2 Answers
macserwicki
8,072 PointsHere is the answer for Part 2. You're only missing the do{} part.
enum ParserError: ErrorType {
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]
do {
let parser = try Parser(data: data)
try parser.parse()
} catch { }
Part 3 Answer Below. Try it on your own before copying :) Just add your 2 error types to the catch{}.
//
//
//
//
//
//
do {
let parser = try Parser(data: data)
try parser.parse()
} catch { ParserError.EmptyDictionary }
catch {
ParserError.InvalidKey
}
Antonija Pek
5,819 PointsI've stuck on part 2 of this challenge.
My code for first task passed but returned errors in second part.
enum ParserError: ErrorType {
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)
Someone help us!
Evans Attafuah
18,277 Pointsthis is my answer and it worked
enum ParserError: ErrorType {
case EmptyDictionary
case InvalidKey
}
struct Parser {
var data: [String : String?]?
func parse() throws {
guard let _ = data else {
throw ParserError.EmptyDictionary
}
guard let _ = data!["somekey"] else {
throw ParserError.InvalidKey
}
}
}
let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)
do {
try parser.parse()
} catch ParserError.EmptyDictionary {
print("empty dictionary")
} catch ParserError.InvalidKey {
print("missing key")
}