I'm not really sure exactly how to describe what I want to do - the best I can do is provide some code as an example:
- (void) doStuffInLoopForDataArray:(NSArray *)arr forObjectsOfClass:(NSString *)class
{
for ([class class] *obj in arr)
{
// Do stuff
}
}
So I might call this like
NSArray *arr = [NSArray arrayWithObjects:@"foo",@"bar", nil];
[self doStuffInLoopForDataArray:arr forObjectsOfClass:@"NSString"];
and I would expect the code to be executed as if I had wrote
- (void) doStuffInLoopForDataArrayOfStrings:(NSArray *)arr
{
for (NSString *obj in arr)
{
// Do KVC stuff
}
}
Is there a way to get this kind of behavior?
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
MyObject1andMyObject2, I could create aBigSuperClass, whereMyObject1andMyObject2are both subclasses ofBigSuperClass.This loop should work for arrays of
MyObject1objects, arrays ofMyObject2objects, or arrays ofBigSuperClassobjects.The more I've been thinking about this, the more I'm leaning towards this being the best approach. Since I can setup my
BigSuperClasswith all the@propertys and methods I'd be interested in as part of my// Do Stuff, which means I won't have to checkrespondsToSelectoras with the other answers. This way just doesn't feel quite as fragile.