Overview:
I have mentioned below a simplified example that causes the error Cannot override mutable property 'a1' of type 'A' with covariant type 'B'
Real Problem:
I have an iOS project in which when similar code is compiled on Xcode (for the simulator)
- Sometimes it compiles ok (not sure if it was the cache)
- Sometimes I get
Command CompileSwift failed with a nonzero exit code
- Sometimes I get
Cannot override mutable property 'a1' of type 'A' with covariant type 'B'
Sorry the above explanation is very vague, when I created a new project with the below classes, I consistently get Cannot override mutable property 'a1' of type 'A' with covariant type 'B'
Note:
- In my real world project when I change the overriding type to be the same, the code consistently compiles fine.
- The overriding property was a subclass of
UITextField
(Based on the above pasted forum link, there was some pointers about how Objective-C might not be as strict as Swift, so I am not sure if it was becauseUITextField
was written in Objective-C)
On the surface it seemed like valid code, but based on In a sub-class, is it possible to override a property of the super-class and constrain to a sub-type of the original property?, I understand that I shouldn't be overriding this way because of the example explained. (table view / scroll view delegate example).
Questions:
- Is the inconsistency in compilation error / success something that has been noticed by others too or is this a problem with my project ?
- Let me know if there is a way to find more from the non-zero error code.
Code:
class A {}
class B : A {}
class Car {
private (set) var a1 = A()
}
class SpecialCar : Car {
override var a1: B {
return B()
}
}