Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Error Handling in Swift 2.0 Error Handling Handling Errors

Error handling swift

Hi Im struggling on the challenge

I know that after the try keyword I have to call the parse function but it wouldn't let me and then I changed the struct brackets to not contain the function and now it lets me use the parse function inside the do clause but it still won't compile and im getting other errors, does anyone know what Ive done wrong

swift_lint.swift:18:19: warning: immutable value 'someKey' was never used; consider replacing with '_' or removing it guard let someKey = data["someKey"] else { ^~~~~~~ _ swift_lint.swift:33:17: error: use of unresolved identifier 'parse' let info = try parse()

error.swift
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 someKey = data["someKey"] else {

            throw ParserError.InvalidKey


        }


  }
}

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

do {
 let info = try parse()

} catch ParserError.EmptyDictionary() {


}

I did it! who :)

No idea how Im still bit unsure so if anyone can go over the logic it would be helpful

do {
 let dataInfo = try Parser.parse(parser)
} catch ParserError.InvalidKey(let someKey) {
print(someKey)

}

1 Answer

Christopher David
PLUS
Christopher David
Courses Plus Student 20,027 Points
do {
 let info = try parse()

} catch ParserError.EmptyDictionary() {

}
do {
 let dataInfo = try Parser.parse(parser)
} catch ParserError.InvalidKey(let someKey) {
print(someKey)

}

The difference in what you did is that you called the instance method from the object when you got it correct. In your first attempt, you tried to call a method from out of the blue - parse() (random method) vs Parser.parse(parser).(method called from object)

Also, I'm not sure because I can't run everything the same in Playground since I am now on Xcode8, but I believe you do not need to set the constant datainfo with let dataInfo = try Parser.parse(parser) since there is no return type to the function/method.

enum ParserError: ErrorType {
  case EmptyDictionary
  case InvalidKey
}

struct Parser {
  var data: [String : String?]?

  func parse() throws {

  guard let dataObject = data else {
    throw ParserError.EmptyDictionary
  }

  guard let keyValue = dataObject["someKey"] else {
    throw ParserError.InvalidKey
  }

  }
}

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

do {
  try parser.parse()
} catch let error {
  print("Error occurred")
}

The above is what I ran. As you can see, I did not set parser.parse() to a constant.