1

Looking at the below code:

public override func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    
    if let urlError = error as? URLError {
        switch urlError.code {
        case .cancelled:
            print("cancelled")
        case .badURL:
            print("badURL")
        default:
            break
        }
    }
}

The first question:

URLError hasn't a property code. It only has a public Struct Code.

enter image description here

So why can use urlError.code.

The second question: URLError.Code is a struct. It has many static property, the code likes below:

enter image description here

It isn't an enum. So why can use syntax case .cancelled:.

Community
  • 1
  • 1
dowZhang
  • 470
  • 1
  • 3
  • 18
  • Where did you get the idea that `switch` can only be used with enums? From the [Swift Programming Language book](https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html), _"A switch statement considers a value and compares it against several possible matching patterns."_. Regarding the first question, it is hard to answer since you haven't included the full definition of URLError. And **don't** post images of code, post it as text – Joakim Danielson Dec 18 '19 at 08:00
  • @JoakimDanielson [URLError](https://developer.apple.com/documentation/foundation/urlerror) is a Foundation class. The docs say it doesn't have a member `code` – JeremyP Dec 18 '19 at 08:34
  • 1
    @JeremyP It wasn't clear from the question but in that case then `code` is defined in `NSError` – Joakim Danielson Dec 18 '19 at 08:45
  • I can't find the relationship between `URLError` and `NSError`. So where does the member `code` defined.@JoakimDanielson @JeremyP – dowZhang Dec 18 '19 at 09:19
  • I have known the reason from [NSError.swift document](https://github.com/apple/swift/blob/master/stdlib/public/Darwin/Foundation/NSError.swift). `URLError` conform the protocol `_BridgedStoredNSError` and this protocol has a member `code`.@JeremyP – dowZhang Dec 19 '19 at 01:22

1 Answers1

1

So why can use urlError.code

According to documentation Error conforms type NSError and NSError has property code, that's why you can use code

It isn't an enum

urlError.code has type URLError.Code and this type conforms protocol RawRepresentable (you can read about it here), that's why you can use switch-case as for enum

Vadim Nikolaev
  • 2,132
  • 17
  • 34
  • Can The protocol `Error` conforms the class `NSError`. Can you give me some documentation which introduce the relationship of `Error` `URLError` and `NSError`.@Vadim Nikolaev – dowZhang Dec 18 '19 at 09:32
  • I would recommend [official Apple document](https://developer.apple.com/documentation/swift/error). Here you can see relationship with NSError, also you can read good info about Error and NSError [here](https://stackoverflow.com/a/40179552/5928311) @dowZhang – Vadim Nikolaev Dec 18 '19 at 11:10
  • 1
    I have known the reason from [NSError.swift document](https://github.com/apple/swift/blob/master/stdlib/public/Darwin/Foundation/NSError.swift). `URLError` conform the protocol `_BridgedStoredNSError` and this protocol has a member `code`.@Vadim Nikolaev – dowZhang Dec 19 '19 at 01:23