Extensions shouldn't override declarationsSW-R1020
Using extensions to override declarations in Swift can lead to unexpected and hard-to-debug behavior. This is because extensions are intended to add new functionality to a type, not to modify or replace existing functionality. Overriding declarations in extensions can cause confusion, as it can be unclear which declaration is in effect at any given time.
For example, if a method is overridden in an extension, it may have different behavior than the original method defined in the class. This can lead to unexpected results when using the method in different contexts.
To fix this issue, it is recommended to avoid overriding declarations in extensions. Instead, modify the original declaration directly or create a new method or property in the class that extends the behavior of the original declaration.
Bad Practice
class Foo {
func bar() {
print("original")
}
}
extension Foo {
override func bar() {
print("overridden")
}
}
Recommended
class Foo {
func bar() {
print("original")
}
}
extension Foo {
func baz() {
print("extended")
}
}