Does @IBAction imply @objc?


(Sebastian Hagedorn) #1

I’m currently migrating some code to the new #selector syntax and came across an issue re: @objc visibility.

I have this method:

@IBAction private func tapGestureRecognized(gesture: UIGestureRecognizer) {…}

…and use this code to set up the recognizer:

let tapSelector = #selector(MyClass.tapGestureRecognized(_:))
let tapGesture = UITapGestureRecognizer(target: self, action: tapSelector)

The previous version with Selector(“…") worked fine. Now that I use #selector, I get this warning:

Argument of '#selector' refers to a method that is not exposed to Objective-C

My understanding was that @IBAction always implied @objc. I can easily fix the warning, either by adding @objc or by removing the private modifier, but I’d like to understand why @objc @IBAction works, but @IBAction does not. I’d like to keep the private modifier. If this is a bug/limitation, I will file a bug report. Just wanted to make sure that I understand it correctly.

Cheers
Sebastian


(Jordan Rose) #2

Hm. There are several levels to this answer:

- The implementation of @IBAction uses @objc.
- In theory we could some day have @IBActions that do not use the ObjC runtime.
- But I agree that having to type "@IBAction @objc private func foo…" seems like a bit much.

Doug, what do you think?

Jordan

···

On Feb 9, 2016, at 3:24 , Sebastian Hagedorn via swift-users <swift-users@swift.org> wrote:

I’m currently migrating some code to the new #selector syntax and came across an issue re: @objc visibility.

I have this method:

@IBAction private func tapGestureRecognized(gesture: UIGestureRecognizer) {…}

…and use this code to set up the recognizer:

let tapSelector = #selector(MyClass.tapGestureRecognized(_:))
let tapGesture = UITapGestureRecognizer(target: self, action: tapSelector)

The previous version with Selector(“…") worked fine. Now that I use #selector, I get this warning:

Argument of '#selector' refers to a method that is not exposed to Objective-C

My understanding was that @IBAction always implied @objc. I can easily fix the warning, either by adding @objc or by removing the private modifier, but I’d like to understand why @objc @IBAction works, but @IBAction does not. I’d like to keep the private modifier. If this is a bug/limitation, I will file a bug report. Just wanted to make sure that I understand it correctly.

Cheers
Sebastian

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Sebastian Hagedorn) #3

May I send a ping regarding this question? :slight_smile:

Thanks for taking a look at this!

···

On 09 Feb 2016, at 18:17, Jordan Rose <jordan_rose@apple.com> wrote:

Hm. There are several levels to this answer:

- The implementation of @IBAction uses @objc.
- In theory we could some day have @IBActions that do not use the ObjC runtime.
- But I agree that having to type "@IBAction @objc private func foo…" seems like a bit much.

Doug, what do you think?

Jordan

On Feb 9, 2016, at 3:24 , Sebastian Hagedorn via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I’m currently migrating some code to the new #selector syntax and came across an issue re: @objc visibility.

I have this method:

@IBAction private func tapGestureRecognized(gesture: UIGestureRecognizer) {…}

…and use this code to set up the recognizer:

let tapSelector = #selector(MyClass.tapGestureRecognized(_:))
let tapGesture = UITapGestureRecognizer(target: self, action: tapSelector)

The previous version with Selector(“…") worked fine. Now that I use #selector, I get this warning:

Argument of '#selector' refers to a method that is not exposed to Objective-C

My understanding was that @IBAction always implied @objc. I can easily fix the warning, either by adding @objc or by removing the private modifier, but I’d like to understand why @objc @IBAction works, but @IBAction does not. I’d like to keep the private modifier. If this is a bug/limitation, I will file a bug report. Just wanted to make sure that I understand it correctly.

Cheers
Sebastian

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users