Skip to content

How can we test respondsToSelector: ? #5

Open

Description

@uchuugaka

First, I am happy to know this is still alive, as it is awesome!
Low level, but awesome.

My question is, what is the approach to test respondsToSelector: ?
I ask because the most common, and tragic mistake I have hit again and again is the stringly selector calling and missing a trailing colon on a selector that takes an argument.
In pure Objective-C this generally isn't a problem because Xcode corrects it most of the time (it is rare to have valid selectors with and without the colon because even Apple/NeXT learned that was troublesome) also because the exeption thrown will have the selector in the stack trace.
Arguably, even the Objective-C runtime could be more forthright about that, but it is also possible the object or its type are not what one expected.

So, I'd like to add this in for debugging (and potentially raise a Pull Request for making it a debug action to check for respondsToSelector: before each call.)

Any tips?

Activity

  1. uchuugaka commented on Apr 3, 2018

    @uchuugaka
    Author

    I think I got it, for anyone else's future reference, or correction if wrong.

    I was trying to figure out which name of finishedLaunching BOOL property of an NSRunningApplication instance to use.
    (of course it responds to both... ugh.)
    But this syntax for calling respondsToSelector: seems to work.
    In particular it seems one needs to import Pointer from jna

    import com.sun.jna.Pointer;
    
    // code that creates an NSRunningApplication instance named runningApp goes here...
    
    String[] selectors = { "isFinishedLaunching", "finishedLaunching" };
    
    for (String aSelector : selectors) {
      Pointer selPointer = ca.weblite.objc.Runtime.INSTANCE.sel_getUid(aSelector);
      System.out.println(String.format("runningApp respondsToSelector \"%s\" ? %b",
                                       aSelector,
                                       runningApp.send("respondsToSelector:", selPointer)));
    }
  2. ind1go commented on Apr 11, 2018

    @ind1go

    Have you had continued success with this method? I've been trying to work out the right selector I need (not an ObjC coder...) and I just get true from everything.

  3. uchuugaka commented on Apr 12, 2018

    @uchuugaka
    Author

    No.
    I cannot get this to line up with my results in Xcode for the same class and methods.
    I still do not know how to properly check respondsToSelector: with this bridge.

  4. LOG-TAG commented on Mar 19, 2019

    @LOG-TAG

    @uchuugaka @shannah any blog post or tutorials available for a beginner developer to run this test code? Add ObjCBridge.jar and jna.jar to your classpath means macOS java classpath via terminal or via xcode?

  5. shannah commented on Mar 19, 2019

    @shannah
    Owner

    There is no need for XCode. ObjCBridge.jar and jna.jar go in your java classpath (e.g. in terminal, or in your IDE, however you build your java project). libjcocoa.dylib needs to be in your java library path.

  6. LOG-TAG commented on Mar 21, 2019

    @LOG-TAG

    Thanks, I will stop bugging you here, are you available in any of the slackgroup for asking more beginner level questions? (email also works if you don't mind)

  7. mojo2012 commented on Jul 29, 2019

    @mojo2012
    Collaborator

    @uchuugaka
    have you tried something like this:

    var appClass = Runtime.INSTANCE.objc_getClass("NSRunningApplication");
    var isFinishedSelPointer = Runtime.INSTANCE.sel_getUid("isFinishedLaunching");
    var isFinishedRetPointer = Runtime.INSTANCE.class_respondsToSelector(appClass, isFinishedSelPointer);

    It returns a Pointer native@0x1 (I guess this means true?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions