swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.6k stars 10.37k forks source link

[SR-6236] Optional methods is not overridable in generic classes #48788

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-6236
Radar None
Original Reporter a.shitikov73 (JIRA User)
Type Bug
Environment Xcode Version 9.0 (9A235), Swift 4 (swiftlang-900.0.63 clang-900.0.37)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 0431123cd21897ee0c5e7783f14807b0

relates to:

Issue Description:

Say you have a base class of UITableViewDelegate - BaseTableViewDelegate and subclass CustomTableViewDelegate:

class BaseTableViewDelegate: NSObject, UITableViewDelegate {
}

class CustomTableViewDelegate: BaseTableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected in subclass")
    }
}

That case works perfectly. But if your BaseTableViewDelegate will have at least one generic parameter - BaseTableViewDelegate\<T> then didSelectRowAt method in subclass will not be called:

class BaseTableViewDelegate<T>: NSObject, UITableViewDelegate {
}

class CustomTableViewDelegate<T>: BaseTableViewDelegate<T> {
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected in subclass")
    }
}
swift-ci commented 7 years ago

Comment by Alexander Shitikov (JIRA)

Very similar issue but without case with generics

belkadan commented 7 years ago

Thanks for reporting. What happens if you explicitly mark the method @objc?

swift-ci commented 7 years ago

Comment by Alexander Shitikov (JIRA)

If I mark it like this:

class CustomTableViewDelegate<T>: BaseTableViewDelegate<T> {
 @objc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected in subclass")
    }
}

Nothing happens. Method doesn't call.

belkadan commented 7 years ago

Ah, right, sorry. Last thing to check, how about if you provide the full selector?

@objc(tableView:didSelectRowAtIndexPath:)

swift-ci commented 2 years ago

Comment by Christopher Snazell (JIRA)

I just ran into this exact situation with XCode 13.1, Swift 5.5.1.

Prefixing an optional method implementation on the subclass with @obj or @objc(full selector) has no effect on the visibility of the method to UITableView when its looking at the delegate or data source.

Is there any ETA for when this might be fixed?

Cheers