gsach
gsach

Reputation: 5775

How to give name on different instances of the same class on iOS - iPhone

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.

Upvotes: 1

Views: 303

Answers (3)

Matthew Frederick
Matthew Frederick

Reputation: 22305

An NSMutableArray of CarCard objects is a much better way to go. Instantiate it outside of your loop:

NSMutableArray *cardArray = [[NSMutableArray alloc] init];

then inside your loop you'll add your objects:

[cardArray addObject:car];

Now you can access them by their indices:

// Card 9, numbering starts at 0
myCar = [cardArray objectAtIndex:8];

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:

NSMutableDictionary *cardDictionary = [[NSMutableDictionary alloc] init];

then inside the loop you'll add your objects:

[cardDictionary setObject:car forKey:cardName];

Now you can access them by their names:

// Card with the key named "card9"
myCar = [cardDictionary objectForKey:@"card9"];

// Card with the key named "monkey"
myCar = [cardDictionary objectForKey:@"monkey"];

Upvotes: 2

FeifanZ
FeifanZ

Reputation: 16316

You could create an NSMutableDictionary and populate it. Assuming you already know (from the plist) how many values you need to create, you could write something like this:

for (int count = 1; count <= totalCars; count++)
     [carDict setObject:[[CarCard alloc] initWithSomeInfo:info] forKey:[NSString stringWithFormat:@"car%d", count]];

Then you could get any numbered object:

CarCard someCard = [carDict objectForKey:[NSString stringWithFormat:@"car%d", selectedNumber]];

Alternatively, you can use NSDictionary's allValues method to get an array of the values:

NSArray *allCards = [carDict allValues];

Upvotes: 0

Mark Granoff
Mark Granoff

Reputation: 16938

Why not create instances of CarCard and simply add them to an array? If you know what N is, then your loop is pretty simple:

NSMutableArray *carCards = [[NSMutableArray alloc] initWithCapacity:N];
for(int i=0; i<N; i++) {
    NSDictionary *car = [NSDictionary dictionaryWithContentsOfFile:<plist-i>];
    CarCard *carCard = [[[CarCard alloc] initWithSomeInfo:car] autorelease];
    [carCards addObject:carCard];
}

Something like this. Is this helpful?

Upvotes: 0

Related Questions