[swift-users] Optional conformance warnings with Protocols
Brent Royal-Gordon
brent at architechies.com
Sun Jul 24 11:05:32 CDT 2016
> On Jul 23, 2016, at 9:09 PM, Rod Brown via swift-users <swift-users at swift.org> wrote:
>
> However, the compiler still emits a warning. "Type of 'title' has different optionality than expected by protocol ‘MKAnnotation' "
Yes, I've encountered this error before. Example:
1> import Foundation
2> @objc protocol P { var x: String? { get } }
3> class X: NSObject, P { let x = "" }
error: repl.swift:3:28: error: type of 'x' has different optionality than required by protocol 'P'
class X: NSObject, P { let x = "" }
^
Swift doesn't appear to allow covariance and contravariance when satisfying protocol requirements, or at least forbids it in optionals. It's a shame, because at least in a theoretical sense it's perfectly consistent.
Unfortunately, you've pretty much just got to do what the compiler tells you in this case. I expect that you'll at least be able to use `!` instead of `?` once ImplicitlyUnwrappedOptional is fully removed, but I don't believe we've gotten there yet.
--
Brent Royal-Gordon
Architechies
More information about the swift-users
mailing list