Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Error work in Swift in Xcode 8 beta 4

Tags:

Seems like when converting our old code to beta 4, I keep casting Error to NSError. That will even lead sometimes to a warning "conditional cast from 'Error' to 'NSError' always succeeds". I feel like I'm not understanding how best to use Error. I want to get to thinks like error.code, error.localizedDescription... Is there good documentation or tutorials that explains these Error changes?

For example:

func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {  

Right now I'm doing something like:

if let error = error as? NSError {  if error.code == NSURLErrorCancelled { 

But that gives the warning "Conditional cast from 'Error' to 'NSError' always succeeds"

like image 436
Jason Hocker Avatar asked Aug 01 '16 21:08

Jason Hocker


People also ask

How do I show errors in Xcode?

In Xcode 9.2 (at least), you can single click on the error / warning icon in the tooltip and it will expand. Show activity on this post. I'm sure if you expand the left hand pane, it shows the full error. Ensure you are in the Warnings/Errors tab.


1 Answers

Error is bridgeable to NSError in the same way that String is bridgeable to NSString. I.e (error as NSError) would work.

if  (error as NSError).code == NSURLErrorCancelled {      // code } 
like image 116
DerrickHo328 Avatar answered Oct 06 '22 01:10

DerrickHo328