tags:

views:

158

answers:

1

I have some obj-c code, i plan to send unsupport messages in plugin. However i cant seem to ignore messages. I thought "-(id) forward: (SEL) selector: (marg_list) arglist;" would solve it but it hasnt. How do i have this app run entirely without getting a warning and a termination call?

#import <stdio.h>
#import <objc/Object.h>

//------- @interface section -------

@interface Fraction: Object
{
    int  numerator;
    int  denominator;
}

-(void)   print;
-(void)   setNumerator: (int) n;
-(void)   setDenominator: (int) d;
-(id)     forward: (SEL) selector: (marg_list) arglist;

@end

//------- @implementation section -------


@implementation Fraction;
-(void) print
{
    printf (" %i/%i ", numerator, denominator);
}

-(void) setNumerator: (int) n
{
    numerator = n;
}

-(void) setDenominator: (int) d
{
    denominator = d;
}

-(id) forward: (SEL) selector: (marg_list) arglist
{
    return nil;
}

@end

//------- program section -------


int main (int argc, char *argv[])
{
    Fraction  *myFraction;

    // Create an instance of a Fraction

    myFraction = [Fraction alloc];
    myFraction = [myFraction init];

    // Set fraction to 1/3

    [myFraction setNumerator: 1];
    [myFraction setDenominator: 3];

    // Display the fraction using the print method

    printf ("The value of myFraction is:");
    [myFraction print];
    printf ("\n");
    [myFraction someMsg: 99];
    [myFraction free];
    printf("end"); 
    return 0;
}
A: 

Rather than handing this in the plugin, it would be best to do it in the caller. Instead of:

[myFraction someMsg: 99];

you should check if myFraction responds to the someMsg selector:

if ([myFraction respondsToSelector:@selector(someMsg:)]) {
    [myFraction someMsg: 99];
}

The -respondsToSelector: method is defined in the NSObject Protocol Reference, so note that your MyFraction object needs to inherit from NSObject, not Object (or else implement the NSObject protocol yourself). Of course, this is really best practice anyway -- directly subclassing the Object class is generally discouraged.

Matt Ball
Directly subclassing Object is more than merely discouraged... nothing actually does it on OS X or in GNUStep. NSObject and NSProxy are both root level objects. Object also does not us retain/release semantics, so passing them around into NSObject based collections and APIs will not work correctly.
Louis Gerbarg