Alex Rivera | Logout

Get rid of "'NSObject' may not respond to '-someMethod'" warning

Asked 2009-07-08T03:00:10.837
10

I'm learning ObjectiveC and ran into a problem relating to introspection. Basically, I'm looping through an array of objects and determining if they accept the lowercaseString selector. If they do, I call that selector on the object. After I ensure that the object responds to that selector, I call it. However, when I do, I get this warning: "warning: 'NSObject; may not respond to '-lowercaseString'"

Although the code works fine as written, I'd like to not get the warning. I'm assuming that there's a "right" way to make sure that I don't get that warning (i.e. without just turning warnings off). Any ideas?

NSMutableArray *myArray = [[NSMutableArray alloc] init];

[myArray addObject:@"Hello!"];
[myArray addObject:[NSURL URLWithString:@"http://apple.com"]];
[myArray addObject:[NSProcessInfo processInfo]];
[myArray addObject:[NSDictionary dictionary]];

SEL lowercaseSelector = @selector(lowercaseString);

for (NSObject *element in myArray) {
    if ([element respondsToSelector:lowercaseSelector]) {
        NSLog([element lowercaseString]); // Warning here
    }
}
Edit
Report

2 Answers

16

You can also use id which is any type of object.

for (id element in myArray) {
    if ([element respondsToSelector:lowercaseSelector]) {
        NSLog([element lowercaseString]);
    }
}
answered 2009-07-08T03:13:17.897
1

Casting as id removed the warning.

if ([self.content respondsToSelector:@selector(setDelegate:)])
{
    [(id)self.content setDelegate:nil];
}
answered 2012-03-05T03:33:29.890

Your Answer