Swift 3 likes to tease me


(Gerriet M. Denkmann) #1

This line (Swift 3):
  if a.responds(to: Selector(“viewControllers") )
creates this warning: Use '#selector' instead of explicitly constructing a 'Selector'

Ok. Following this advice I change it to:
  if a.responds(to: #selector(“viewControllers"))
and now get an error instead: Argument of ‘#selector' does not refer to an '@objc' method, property, or initializer

Why do I get punished for following Xcode’s advice?

Gerriet.


(Jacob Bandes-Storch) #2

#selector is not used with a string, but with an actual reference to a
method. If, for instance, you have a protocol MyVC which declares `var
viewControllers: ...` then you can use something like
#selector(MyVC.viewControllers).

···

On Thu, Sep 22, 2016 at 9:51 PM, Gerriet M. Denkmann via swift-users < swift-users@swift.org> wrote:

This line (Swift 3):
        if a.responds(to: Selector(“viewControllers") )
creates this warning: Use '#selector' instead of explicitly constructing a
'Selector'

Ok. Following this advice I change it to:
        if a.responds(to: #selector(“viewControllers"))
and now get an error instead: Argument of ‘#selector' does not refer to an
'@objc' method, property, or initializer

Why do I get punished for following Xcode’s advice?

Gerriet.

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


(Joe Groff) #3

This line (Swift 3):
  if a.responds(to: Selector(“viewControllers") )
creates this warning: Use '#selector' instead of explicitly constructing a 'Selector'

Ok. Following this advice I change it to:
  if a.responds(to: #selector(“viewControllers"))
and now get an error instead: Argument of ‘#selector' does not refer to an '@objc' method, property, or initializer

Others have answered your question, but this looks to me like an opportunity for us to improve the error message. Attempting to use #selector() with a string literal like this seems like a natural thing to try, and we could offer better diagnostics for this case. If you have a moment, would you be able to file a bug to improve the diagnostics in this case?

-Joe

···

On Sep 22, 2016, at 9:51 PM, Gerriet M. Denkmann via swift-users <swift-users@swift.org> wrote:

Why do I get punished for following Xcode’s advice?

Gerriet.

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


(Gerriet M. Denkmann) #4

This might be difficult. viewControllers is a property used in several classes, e.g. UIPageViewController, UISplitViewController, etc.

Xcode has just given me a new suggestion (probably it noticed that I was not quite happy with its former hint):
  if a.responds(to: #selector(getter: UINavigationController.viewControllers))
but I do not want to be restricted to this special class.
My a is AnyObject and should be treated as such.

Kind regards,

Gerriet.

···

On 23 Sep 2016, at 12:09, Marco S Hyman <marc@snafu.org> wrote:

On Sep 22, 2016, at 9:51 PM, Gerriet M. Denkmann via swift-users <swift-users@swift.org> wrote:

This line (Swift 3):
  if a.responds(to: Selector(“viewControllers") )
creates this warning: Use '#selector' instead of explicitly constructing a 'Selector'

Ok. Following this advice I change it to:
  if a.responds(to: #selector(“viewControllers”))

#selector does not take a string. It takes a method name. This is required as the name needs to be mangled/demangled in the conversion between obj-c and swift.

You might need the class name, too, e.g. #selector(MyClass.viewControllers)


(Adam Eberbach) #5

Xcode will be happy if you add “@objc” in front of your “func” declaration.

···

On 23 Sep 2016, at 2:51 PM, Gerriet M. Denkmann via swift-users <swift-users@swift.org> wrote:

This line (Swift 3):
  if a.responds(to: Selector(“viewControllers") )
creates this warning: Use '#selector' instead of explicitly constructing a 'Selector'

Ok. Following this advice I change it to:
  if a.responds(to: #selector(“viewControllers"))
and now get an error instead: Argument of ‘#selector' does not refer to an '@objc' method, property, or initializer

Why do I get punished for following Xcode’s advice?

Gerriet.

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


(Jacob Bandes-Storch) #6

Once you've determined this object responds to the selector, how do you
intend to use it?

It's probably best to define your own protocol that has the requirement,
and then you can access it via the protocol. In fact, at that point you
might even be able to use "as?" rather than "respondsToSelector".

What are you trying to do exactly?

Jacob

···

On Thu, Sep 22, 2016 at 11:57 PM, Gerriet M. Denkmann <g@mdenkmann.de> wrote:

> On 23 Sep 2016, at 12:09, Marco S Hyman <marc@snafu.org> wrote:
>
> On Sep 22, 2016, at 9:51 PM, Gerriet M. Denkmann via swift-users < > swift-users@swift.org> wrote:
>>
>> This line (Swift 3):
>> if a.responds(to: Selector(“viewControllers") )
>> creates this warning: Use '#selector' instead of explicitly
constructing a 'Selector'
>>
>> Ok. Following this advice I change it to:
>> if a.responds(to: #selector(“viewControllers”))
>
> #selector does not take a string. It takes a method name. This is
required as the name needs to be mangled/demangled in the conversion
between obj-c and swift.
>
> You might need the class name, too, e.g. #selector(MyClass.
viewControllers)

This might be difficult. viewControllers is a property used in several
classes, e.g. UIPageViewController, UISplitViewController, etc.

Xcode has just given me a new suggestion (probably it noticed that I was
not quite happy with its former hint):
        if a.responds(to: #selector(getter: UINavigationController.
viewControllers))
but I do not want to be restricted to this special class.
My a is AnyObject and should be treated as such.

Kind regards,

Gerriet.


(Gerriet M. Denkmann) #7

Once you’ve determined this object responds to the selector, how do you intend to use it?

The function containing “ if a.responds…” is helping me to understand the layout of views in iOS.
The input is some object, and if this object responds to “viewControllers” the function will print some info for all these viewControllers.
It is only used for debugging.

···

On 23 Sep 2016, at 14:19, Jacob Bandes-Storch via swift-users <swift-users@swift.org> wrote:

It's probably best to define your own protocol that has the requirement, and then you can access it via the protocol. In fact, at that point you might even be able to use "as?" rather than "respondsToSelector".

What are you trying to do exactly?

Jacob

On Thu, Sep 22, 2016 at 11:57 PM, Gerriet M. Denkmann <g@mdenkmann.de> wrote:

> On 23 Sep 2016, at 12:09, Marco S Hyman <marc@snafu.org> wrote:
>
> On Sep 22, 2016, at 9:51 PM, Gerriet M. Denkmann via swift-users <swift-users@swift.org> wrote:
>>
>> This line (Swift 3):
>> if a.responds(to: Selector(“viewControllers") )
>> creates this warning: Use '#selector' instead of explicitly constructing a 'Selector'
>>
>> Ok. Following this advice I change it to:
>> if a.responds(to: #selector(“viewControllers”))
>
> #selector does not take a string. It takes a method name. This is required as the name needs to be mangled/demangled in the conversion between obj-c and swift.
>
> You might need the class name, too, e.g. #selector(MyClass.viewControllers)

This might be difficult. viewControllers is a property used in several classes, e.g. UIPageViewController, UISplitViewController, etc.

Xcode has just given me a new suggestion (probably it noticed that I was not quite happy with its former hint):
        if a.responds(to: #selector(getter: UINavigationController.viewControllers))
but I do not want to be restricted to this special class.
My a is AnyObject and should be treated as such.

Kind regards,

Gerriet.

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