views:

194

answers:

1

I have create a class from a string, check it is valid and then check if it responds to a particular method. If it does then I call the method. It all works fine, except I get an annoying compiler warning: "warning: no '-setCurrentID:' method found". Am I doing something wrong here? Is there anyway to tell the compiler all is ok and stop it reporting a warning?

The here is the code:

// Create an instance of the class
id viewController = [[NSClassFromString(class) alloc] init];

// Check the class supports the methods to set the row and section
if ([viewController respondsToSelector:@selector(setCurrentID:)]) 
    {
        [viewController setCurrentID:itemID];
    }   

// Push the view controller onto the tab bar stack      
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];

Cheers

Dave

+3  A: 

Either import the header which declares the method or just use an informal protocol in your implementation to declare it. The compiler has to know the signature of the method.

@interface NSObject (MyInformalProtocol)

- (void)setCurrentID:(int)id;

@end
Nikolai Ruhe
Thanks Nikolai, very helpful. CheersDave
Magic Bullet Dave
For the differences between formal and informal protocols, this is a good reference, again thanks for the steer. http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/articles/ocProtocols.htmlRegards Dave
Magic Bullet Dave
There's another way to get around the warning that I sometimes use when I'm lazy: NSObject's `performSelector:withObject:` instead of sending the message directly. But that's only good when your argument is an object.
Nikolai Ruhe
Glad I could help an old-time-spectrum-kid like me.
Nikolai Ruhe