Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-9391] Incorrect fix-it when trying to use a selector inside an extension of a non-objc protocol #51857

Closed
MartinP7r opened this issue Dec 1, 2018 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@MartinP7r
Copy link

Previous ID SR-9391
Radar None
Original Reporter @MartinP7r
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee @theblixguy
Priority Medium

md5: d624f9ffd7a333e72fcf94668d9a3359

Issue Description:

First time post, so kindly let me know if I'm going against any guidelines.

The below code prompts an error in Xcode with quick-fix stating:

Argument of '#selector' refers to instance method 'btnHandler(sender: ))' that is not exposed to Objective-C``Add '@objc' to expose this instance method to Objective-C

However, applying the quick-fix adds `@objc` to the btnHandler line in the protocol definition instead of the instance method.

import UIKit

protocol SomeProtocol {
    func setupButton()
    func btnHandler(sender: UIBarButtonItem)
}

extension SomeProtocol {

    func setupButton() {
        let button = UIBarButtonItem()
        button.action = #selector(btnHandler)
    }

    func btnHandler(sender: UIBarButtonItem) { }
}

I know that @objc can't be used in non-concrete extension, however the syntax auto-correct behavior seems odd.

edit: fixed mistake in example, pointed out by @theblixguy

@theblixguy
Copy link
Collaborator

There's a small mistake in your example: you're setting the `action` on a button that hasn't been initialised yet.

@MartinP7r
Copy link
Author

Thank you for pointing that out, I fixed the example code.

However, you can also insert the fixit on the protocol declaration (`@objc protocol SomeProtocol {}`).

That actually solved the problem I was trying to solve while stumbling over the misplaced fixit. Thank you! 😃

@theblixguy
Copy link
Collaborator

Here's the PR for the fix: #20949

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

2 participants