ObjC vs Swift action selectors


(David Catmull) #1

I have a unit test in which I verify that a view controller is correctly
validating items in a context menu. I converted the view controller class
to Swift, keeping the selector names the same, while the test is still in
ObjC. The test now doesn't work because the selector created in ObjC as
@selector(action:) somehow isn't matching Swift's
#selector(ClassName.action(_:)). Do I have to rewrite my test in Swift for
this to work?


(Joe Groff) #2

You could try giving ClassName.action(_:slight_smile: an explicit selector using the @objc(action:) attribute to ensure that it's consistent with the ObjC code. If that doesn't work, then we may have a bug in the compiler.

-Joe

···

On Dec 6, 2016, at 8:54 AM, David Catmull via swift-users <swift-users@swift.org> wrote:

I have a unit test in which I verify that a view controller is correctly validating items in a context menu. I converted the view controller class to Swift, keeping the selector names the same, while the test is still in ObjC. The test now doesn't work because the selector created in ObjC as @selector(action:) somehow isn't matching Swift's #selector(ClassName.action(_:)). Do I have to rewrite my test in Swift for this to work?


(David Catmull) #3

I tried, writing that as @objc(action:) #selector(ClassName.action(_:)) but
that got an "expected declaration" error.

I also tried adding @objc(action:) to the method itself (which seems
redundant anyway), but that didn't help either.

>
> I have a unit test in which I verify that a view controller is

correctly validating items in a context menu. I converted the view
controller class to Swift, keeping the selector names the same, while the
test is still in ObjC. The test now doesn't work because the selector
created in ObjC as @selector(action:) somehow isn't matching Swift's
#selector(ClassName.action(_:)). Do I have to rewrite my test in Swift for
this to work?

You could try giving ClassName.action(_:slight_smile: an explicit selector using the

@objc(action:) attribute to ensure that it's consistent with the ObjC code.
If that doesn't work, then we may have a bug in the compiler.

···

On Tue, Dec 6, 2016 at 11:50 AM, Joe Groff <jgroff@apple.com> wrote:

> On Dec 6, 2016, at 8:54 AM, David Catmull via swift-users < swift-users@swift.org> wrote:

-Joe


(Joe Groff) #4

The attribute has to go on the method definition. Would you be able to file a bug on bugs.swift.org <http://bugs.swift.org/> with your Swift and ObjC code?

-Joe

···

On Dec 6, 2016, at 12:53 PM, David Catmull <davidcatmull@gmail.com> wrote:

I tried, writing that as @objc(action:) #selector(ClassName.action(_:)) but that got an "expected declaration" error.

I also tried adding @objc(action:) to the method itself (which seems redundant anyway), but that didn't help either.


(David Catmull) #5

I created a simple test project, and found that it didn't fail. Then I
looked at my original test again and realized it was actually failing for
other reasons. So never mind :slight_smile:

···

On Tue, Dec 6, 2016 at 1:55 PM, Joe Groff <jgroff@apple.com> wrote:

On Dec 6, 2016, at 12:53 PM, David Catmull <davidcatmull@gmail.com> wrote:

I tried, writing that as @objc(action:) #selector(ClassName.action(_:))
but that got an "expected declaration" error.

I also tried adding @objc(action:) to the method itself (which seems
redundant anyway), but that didn't help either.

The attribute has to go on the method definition. Would you be able to
file a bug on bugs.swift.org with your Swift and ObjC code?

-Joe