Hi,
I got an issue while developing iOS app, and I thought to ask here too since the issue happens between different Swift versions, maybe someone can explain the cause.
I have this sample code:
protocol Proto { var value: String { get } }
extension URLError: Proto { var value: String { return "hello" } }
func value(_ error: Error) {
if let error = error as? Proto {
print("proto", error.value)
} else {
print(":(")
}
}
I except with the runtime-polymorphism the casting should work, and following call should print proto hello
when passing URLError
object:
value(URLError(.badURL))
But here is what I got, in Swift 5.0 the casting works, in Swift 5.3 the casting does not work.
Here trying replit.com for personal testing and the casting works:
Their swift version is 5.0.x as shown:
Swift version 5.0.1 (swift-5.0.1-RELEASE)
When I tested the code on local playground, the casting doesn't work, and my swift version is:
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Also someone tested it with Swift 5.4 on his machine, and the casting didn't work for him too.
I hope if there is an answer to this case to be explained as simple as possible for a beginner.
Thanks