Another approach would be to create a single superclass that all the classes I'd like to use this method for inherit from. I can then loop using that superclass.
So if I want to be able to loop for MyObject1
and MyObject2
, I could create a BigSuperClass
, where MyObject1
and MyObject2
are both subclasses of BigSuperClass
.
- (void) doStuffInLoopForDataArray:(NSArray *)arr
{
for (BigSuperClass *obj in arr)
{
// Do stuff
}
}
This loop should work for arrays of MyObject1
objects, arrays of MyObject2
objects, or arrays of BigSuperClass
objects.
The more I've been thinking about this, the more I'm leaning towards this being the best approach. Since I can setup my BigSuperClass
with all the @property
s and methods I'd be interested in as part of my // Do Stuff
, which means I won't have to check respondsToSelector
as with the other answers. This way just doesn't feel quite as fragile.