(Solved) Ch 7 Warning: Conditional downcast from 'Error?' to 'NSError'

There are a couple places in CampgroundManager and CampgroundManagerTests where…

if let error = error as? NSError…

…results in the following compiler warning:

Conditional downcast from ‘Error?’ to ‘NSError’ is a bridging conversion; did you mean to use ‘as’?

Is there a good way to resolve this?
Thanks…

Jerry

Apparently the solution was to change the line to…

if let error = error as NSError?

Jerry

1 Like

Hi, Jerry!
If you use do-catch swift statement, for catching any errors you should type something like:

// Swift 3.1
do {
  try doSomethingWithPossibleErrors()
} catch let error as NSError {
  doSomethingIfErrorAppears()
}

, where catch construction expects error-type variable which you can use inside curly braces after catching. For instance, you can print out the error description like below.

fatalError("Error: \(error), Description: \(error.userInfo)")