Question

I'm having a problem with respondsToSelector with NSMutableSet.

I have a program like this:

NSArray *arguments = [[NSProcessInfo processInfo] arguments];
theClass = [arguments objectAtIndex: 1];
theMethod = [arguments objectAtIndex: 2];
theArgument = [arguments objectAtIndex: 3];

id object = [[NSClassFromString(theClass) init] autorelease];

if([object respondsToSelector:NSSelectorFromString(theMethod)]) {
    NSLog(@"Result: %@",
    [object performSelector:NSSelectorFromString(theMethod) withObject: theArgument]);
} else {
    NSLog(@"Class %@ doesn't respond to %@.",
    theClass, theMethod);
}

I call it using ./program NSMutableSet addObject: str, but the program always says that NSMutableSet doesn't respond to addObject:.

I don't know why respondsToSelector always says that NSMutableSet doesn't respond to addObject. It's the same with ./program NSMutableSet allObjects.

Was it helpful?

Solution

You were checking something completely different...

[NSClassFromString(theClass) init]

needs to be

[[NSClassFromString(theClass) alloc] init]

instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top