-
Notifications
You must be signed in to change notification settings - Fork 25
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
How can we test respondsToSelector: ? #5
Comments
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. 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)));
} |
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 |
No. |
@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? |
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. |
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) |
@uchuugaka 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 |
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?
The text was updated successfully, but these errors were encountered: