[SR-11905] Objective-C interop allows creation of undefined behavior
#54,322 建立於 2019年12月5日
描述
| Previous ID | SR-11905 |
| Radar | rdar://problem/57711780 |
| Original Reporter | @DevAndArtist |
| Type | Bug |
Apple Swift version 5.1.2 (swiftlang-1100.0.278 clang-1100.0.33.9)
| Votes | 1 |
| Component/s | Compiler |
| Labels | Bug, StarterBug |
| Assignee | faical (JIRA) |
| Priority | Medium |
md5: df1a976b8eb12c28ba3696248c7605f6
Issue Description:
In pure Swift this wouldn't be possible because we shouldn't be able to override a property of a class we import and don't own.
extension UITabBarController {
open override var shouldAutorotate: Bool {
return true
}
}
However due to Objective-C interop the swift compiler does not complain about this extension and let us create an undefined behavior at runtime.
If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. This is less likely to be an issue if you’re using categories with your own classes, but can cause problems when using categories to add methods to standard Cocoa or Cocoa Touch classes.