Reputation: 490
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
.
So why can use urlError.code
.
The second question:
URLError.Code
is a struct. It has many static property, the code likes below:
It isn't an enum. So why can use syntax case .cancelled:
.
Upvotes: 2
Views: 839
Reputation: 2132
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
Upvotes: 1