I am iterating through an NSArray that contains many different types of objects. There are many methods to figure out what class the object is. However, I haven't been able to find a good way to find out if an object can implement a particular function. I can put it in a try-catch but it will still output an error message in the console even if I'm catching the error. Is there a better way to do this?
Simple example:
@try {
if ([element lowercaseString]) {
//do something
}
}
@catch (id theException) {
// do something else
}
As suggested, you can use respondsToSelector:
message declared on NSObject
. The provided code would be like
if ([element respondsToSelector:@selector(lowercaseString)]) {
// ... do work
}
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