Dragna
(Sébastien)
1
We had a piece of code that was working without a problem before, but now when I compile with the latest beta I get this error (this was working with beta 1).
here is the code
import UIKit
extension Optional where Wrapped: UIView {
var isHidden: Bool {
guard let unwrappedSelf = self else { return true }
return unwrappedSelf.isHidden
}
}
protocol MyViewProtocol: UIView {}
class MyView: UIView, MyViewProtocol {}
let view: MyViewProtocol? = MyView()
view.isHidden
the error I get from the compiler are these :
xpression failed to parse:
error: compilerProblemXcode14.playground:15:1: error: property 'isHidden' requires that 'any MyViewProtocol' inherit from 'AnyObject'
view.isHidden
^
compilerProblemXcode14.playground:3:1: note: where 'Wrapped' = 'any MyViewProtocol', 'UIView' = 'AnyObject'
extension Optional where Wrapped: UIView {
^
I tried adding the AnyObject to the MyViewProtocol but it doesn't work.
I can workaround by not using my extension or casting my view as a UIView. or doing this
view?.isHidden ?? true
but I don't understand why the compiler is blocking us, we are surely doing something wrong but I can't see what.
Thanks for anyone that will take time to read this
.
(I sadly can't update the playground)
pierremb
(Pierre Monod-Broca)
2
I thought constraining a protocol to a specific class wasn't possible.
Have you tried:
protocol MyViewProtocol: AnyObject {}
let view: (UIView & MyViewProtocol)? = MyView()
or with a typealias:
protocol MyProtocol: AnyObject {}
typealias MyViewProtocol = UIView & MyProtocol
let view: MyViewProtocol? = MyView()
Dragna
(Sébastien)
3
It was actually working fine before with Xcode 14 beta 1 and previous version of Xcode.
I tried with your suggestion (thank you for that by the way
), but no luck, I keep having the same error 