I am developing an iPhone application which has a lot of different cards, each one of those represents a car. So, every card has some instance variables and one image. My class CarCard is loading data for each car from a .plist file. I want to make many instances in a loop so as to give different name to each instance. E.g:
CarCard *car1 = [[CarCard alloc] initWithSomeINfo: info];
....... *car2 ..........................................;
....... *carN ..........................................;
where N is the number of Cars. The problem is that I do not know what the number N is each time. So i have to make the instances in a loop, but i could not find how to give different names to instances.
An
NSMutableArrayofCarCardobjects is a much better way to go. Instantiate it outside of your loop:then inside your loop you’ll add your objects:
Now you can access them by their indices:
Edited to add
If you want to refer to the cards by their name instead of their position in the array and the names will be unique, you can use a dictionary and refer to the cards by name. Instantiate the dictionary outside of the loop:
then inside the loop you’ll add your objects:
Now you can access them by their names: