iPhone: How to create dynamic instances for any built-in class object?

advertisements

I want to create dynamic instances for a built in class object. I am not sure how to achieve this. For example, the below audio player object instance i want to create dynamically, so that I can play dynamic multiple data.

audioPlayer = [[AVAudioPlayer alloc] initWithData:pData error:nil];

instead of this, i want to be something like below,

audioPlayer1 = [[AVAudioPlayer alloc] initWithData:pData error:nil];
audioPlayer2 = [[AVAudioPlayer alloc] initWithData:pData error:nil];
audioPlayer3 = [[AVAudioPlayer alloc] initWithData:pData error:nil];

Here, 1, 2, 3 can be added dynamically, because i don't know how many i have to add at once, it has to be dynamically created.

I though i can do like,

AVAudioPlayer *[NSString stringWithFormat:@"audioPlayer%d", value] = [[AVAudioPlayer alloc] initWithData:pData error:nil];

But this is not acceptable.

Could someone help me on this to resolve?


Just use a loop to create the instances and put them into a mutable array.

NSMutableArray *players = [NSMutableArray array];
for (int i = 0; i < 3; i++) {
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:pData error:nil];
    [players addObject:audioPlayer];
}