Can this be replaced by a cycle?
Animal* leon = [[Leon alloc]init]; Animal* zebra = [[Zebra alloc]init]; Animal* kenguru = [[Kenguru alloc]init]; Animal* mouse = [[Mouse alloc]init]; Animal* penguin = [[Penguin alloc]init]; Can this be replaced by a cycle?
Animal* leon = [[Leon alloc]init]; Animal* zebra = [[Zebra alloc]init]; Animal* kenguru = [[Kenguru alloc]init]; Animal* mouse = [[Mouse alloc]init]; Animal* penguin = [[Penguin alloc]init]; If you store all the objects in the array is suitable, you can do so
NSArray *listOfAnimals = @[@"Zebra", @"Penguin"]; NSMutableArray *listOfAnimalInstances = [NSMutableArray new]; for(int i = 0; i<listOfAnimals.count; i++) { [listOfAnimalInstances addObject:[NSClassFromString(listOfAnimals[i]) new]]; } Source: https://ru.stackoverflow.com/questions/611792/
All Articles
element.instanceOf()over the elements of the array and do something likeelement.instanceOf()on each (somehow in Java, but the same syntax should be in Obj-C). But why? You will make the code much less understandable. - Nick Volynkin ♦