Replacing a method via categorys

  • I'm trying to replace a method in NSCell, via categories.

    First off, let me say I know this is usually a very bad thing to do.
    Mostly if done wrong, I can introduce bugs into perfectly good code.
    And also if Apple ever change NSCell, my code might break in the future.

    Anyhow, just for experiments sake and assuming I'm responsible for
    the dangers, I have this question.

    If I want to replace a method in a category, how can I get that
    replacement method to call the original method?

    @implementation NSCell (NoHilite)
    - (void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:
    (NSView *)controlView {
        if (controlView != IgnoredHilite) {
            [super highlight:flag withFrame:cellFrame inView:controlView];
        }
    }

    @end

    Here I get a compile time error that [super highlight:flag
    withFrame:cellFrame inView:controlView]; is bad. I'm not sure if it
    is possible even to call the original method.

    Also, the method doesn't even get called (I set breakpoints), and my
    cells get hilited normally.

    Any clues anyone?

    --
    http://elfdata.com/plugin/ Industrial strength string processing,
    made easy.

    "All things are logical. Putting free-will in the slot for premises in
    a logical system, makes all of life both understandable, and free."
  • Theodore,

    On 27.6.2005, at 23:48, Theodore H. Smith wrote:

    > If I want to replace a method in a category, how can I get that
    > replacement method to call the original method?

    In short: you can't. That's the very principle of categories.

    (Well in fact there are somewhat dirty runtime-based tricks to do
    that, but do avoid them unless you have no other option :))
    ---
    Ondra ÄŒada
    OCSoftware:    <ocs...>              http://www.ocs.cz
    private        <ondra...>            http://www.ocs.cz/oc
  • You may want to read this - http://cocoadev.com/index.pl?MethodSwizzling
    It explains exactly what you want.

    - Ofri

    On 28/06/2005, at 00:48, Theodore H. Smith wrote:

    >
    > I'm trying to replace a method in NSCell, via categories.
    >
    > First off, let me say I know this is usually a very bad thing to
    > do. Mostly if done wrong, I can introduce bugs into perfectly good
    > code. And also if Apple ever change NSCell, my code might break in
    > the future.
    >
    > Anyhow, just for experiments sake and assuming I'm responsible for
    > the dangers, I have this question.
    >
    > If I want to replace a method in a category, how can I get that
    > replacement method to call the original method?
    >
    >
    > @implementation NSCell (NoHilite)
    > - (void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:
    > (NSView *)controlView {
    > if (controlView != IgnoredHilite) {
    > [super highlight:flag withFrame:cellFrame inView:controlView];
    > }
    > }
    >
    > @end
    >
    > Here I get a compile time error that [super highlight:flag
    > withFrame:cellFrame inView:controlView]; is bad. I'm not sure if it
    > is possible even to call the original method.
    >
    > Also, the method doesn't even get called (I set breakpoints), and
    > my cells get hilited normally.
    >
    > Any clues anyone?
    >
    > --
    > http://elfdata.com/plugin/ Industrial strength string processing,
    > made easy.
    >
    > "All things are logical. Putting free-will in the slot for premises in
    > a logical system, makes all of life both understandable, and free."
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/ofri.wolfus%
    > 40gmail.com
    >
    > This email sent to <ofri.wolfus...>
    >
  • 27 jun 2005 kl. 23.48 skrev Theodore H. Smith:
    >
    > I'm trying to replace a method in NSCell, via categories.

    Instead of swizzling, I suspect that in your case it will be much
    easier to use a custom sub-class and tell your table view to use that
    class.

    > Here I get a compile time error that [super highlight:flag
    > withFrame:cellFrame inView:controlView]; is bad. I'm not sure if it
    > is possible even to call the original method.

    There is no highlight:withFrame:inView: method in NSObject, so that
    is expected.

    .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
    .  .  .  .  .  .  .  .  .  .  .  .
    . <waterspirit...> .  . www.synapticpulse.net .
  • On 28 Jun 2005, at 12:49, Pandaa wrote:

    >
    > 27 jun 2005 kl. 23.48 skrev Theodore H. Smith:
    >
    >>
    >> I'm trying to replace a method in NSCell, via categories.
    >>
    >
    > Instead of swizzling, I suspect that in your case it will be much
    > easier to use a custom sub-class and tell your table view to use
    > that class.

    Well, subclassing avoids hacking. Swizzling is a pretty serious hack.
    So I've decided to subclass, instead.

    --
    http://elfdata.com/plugin/ Industrial strength string processing,
    made easy.

    "All things are logical. Putting free-will in the slot for premises in
    a logical system, makes all of life both understandable, and free."
  • At 11:24 Uhr +0300 28.06.2005, Ofri Wolfus wrote:
    > You may want to read this - http://cocoadev.com/index.pl?MethodSwizzling
    > It explains exactly what you want.

    Don't. Use poseAsClass instead. Method swizzling only works as
    expected if the method to be swizzled actually exists in the class
    you're working on. If it's a method that was inherited from a
    superclass, you'll end up replacing the superclass's method -- ouch.

    With poseAsClass, you effectively just subclass the class and tell
    the rest of the app to use your subclass whenever it asks for the
    superclass. So, to call the original method (whether inherited or
    not), you can simply call super.
    --
    Cheers,
    M. Uli Kusterer
    ------------------------------------------------------------
            "The Witnesses of TeachText are everywhere..."
                        http://www.zathras.de
previous month june 2005 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Go to today