Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
    }
}
like image 244
Tyson Avatar asked Jul 08 '09 03:07

Tyson


1 Answers

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

for (id element in myArray) {
    if ([element respondsToSelector:lowercaseSelector]) {
        NSLog([element lowercaseString]);
    }
}
like image 59
Roland Rabien Avatar answered Sep 29 '22 07:09

Roland Rabien