Reputation: 6263
I can't understand, why in some cases I got error "Declarations in extensions cannot override" and in other cases - not.
Code:
protocol ConstrTest {
var goodConstraints: [NSLayoutConstraint] {get}
var badConstraints: [NSLayoutConstraint?] {get}
}
extension UIViewController: ConstrTest
{
var goodConstraints: [NSLayoutConstraint] {
return []
}
var badConstraints: [NSLayoutConstraint?] {
return []
}
}
class TestViewController: UIViewController {
override var goodConstraints: [NSLayoutConstraint] {
return []
} //No errors and it works
override var badConstraints: [NSLayoutConstraint?] {
return []
} //Got error Declarations in extensions cannot override
}
Why I can overide an array and can't override an optionals array?
Upvotes: 1
Views: 152
Reputation: 42143
You cannot override a variable or function that you added to the base class using an extension. This may become possible in a future version of Swift.
The compiler may let you off with pure (or bridged) Objective-C types but as soon as you use a Swift type (e.g. an Optional or an Enum, etc.) you're going to get a compilation error.
Upvotes: 1