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
}
}
You can also use id
which is any type of object.
for (id element in myArray) {
if ([element respondsToSelector:lowercaseSelector]) {
NSLog([element lowercaseString]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With