omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Call objc methods that contain an underscore

    Pythonista
    objcutil objc
    2
    2
    1853
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • lukaskollmer
      lukaskollmer last edited by

      How can I use objc_util to call Objective-C methods that contain underscores?

      Usually, I'd write object._doSomethingWithParameter_(param), but the problem is that only the second underscore indicates a parameter. The first one is part of the method name. Pythonista however will convert that to the selector :doSomethingWithParameter:, which requires two parameters.

      How do I call the following method:

      - (void)_addActionWithTitle:(id)arg1 image:(id)arg2 style:(int)arg3 handler:(id)arg4;
      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by omz

        objc_util should usually be smart enough to figure this out automatically. If you attempt to call _doSomethingWithParam_, it will first try to translate that to :doSomethingWithParameter:, but if that method doesn't exist (which is very likely), it'll try all permutations of underscores and colons for replacing the underscores in the method name, and it should eventually arrive at _doSomethingWithParameter: (in this case, there are only 4 possible permutations).

        There could theoretically be cases where both _doSomethingWithParameter and :doSomethingWithParameter: are valid methods (unlikely, but possible) – in those cases, you could construct an ObjCInstanceMethod manually like this: ObjCInstanceMethod(some_obj, '_doSomethingWithParameter:'). This would also be slightly faster because there's no guessing involved to figure out the correct selector.

        Long story short, calling _addActionWithTitle_image_style_handler_ should "just work".

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Powered by NodeBB Forums | Contributors