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 trialErman Sahin Tatar
3,223 PointsError Handling in Swift, How can I know whether key is in Dictionary or not ?
Question is asking that detect particular key in dictionary and if it has not the key then throw Invalid Key error. but I canot do, I did some research at google but solution gives error in editor. Can you help me?
[Wrong part is that detecting key is in dictionary or it is invalid key?]
enum ParserError: ErrorType {
case EmptyDictionary
case InvalidKey
}
struct Parser {
var data: [String : String?]?
func keys(data: [String: String?]?)-> [String]{
let array: [String] = data.keys
return array
}
func parse() throws {
guard let dict = data else {
throw ParserError.EmptyDictionary
}
let temp = keys(data)
for i in temp{
if temp[i] != data[String]{
throw ParserError.InvalidKey
}
}
}
}
let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)
3 Answers
Michael Reining
10,101 PointsHi Erman,
The error handling can be trickly. There is a new syntax to follow that looks like this.
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 { }
The resource below helped me get more comfortable with it.
https://www.bignerdranch.com/blog/error-handling-in-swift-2/
I hope that helps,
Mike
PS: Thanks to the awesome resources on Team Treehouse, I launched my first app.
Now you can practice writing Swift code directly on your iPhone :-)
Erman Sahin Tatar
3,223 PointsHi Micheal, thank you very much for helping and showing resources! How can I reach all resources which are in threehouse, (I noted all links which was shared in teacher's notes ) are there any other part that show resources about topic ? I really want to make my app but I think Swift IOS Development track is so basic and not enough to build strong app, can you give me advice what should I read or whatch to improve my swift skills to build beautiful app :) I need your opinion thanks!
Anthia Tillbury
3,388 PointsI think the majority of the Swift specific information in the article was already covered in the module. The only things I picked up on that were left out were that the list of caught errors would have to be exhaustive, as with case statements. And the use of the "Bang Operator" (!) on a try statement, of which Passan would never want to teach anyway.
At the time of writing the version of Swift was only 2.0 (it's 4.0 at the present time of writing) and so this error handling was new and switching over from C I believe, so there was a lot of history covered in the article. However, I still don't see how you managed to compile your code from that article, it didn't open anything new up to me.
Good luck on your new app by the way.
Martel Storm
4,157 PointsMartel Storm
4,157 PointsWhere can I check out this app of yours? :)