Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing arrays in Objective-C trying to simplifying a lot of repeating code
    text
    copied!<p>Hello StackOverflow gurus. This is my first question on here so I am excited to jump right in.</p> <p>I am trying to understand iOS arrays a little better and I've hit a brick wall. I am making a sound app that is using FMOD. I have everything working perfectly but I have 9 buttons that all perform nearly the exact same thing except each play a different .wav file on press then on release stop that sound. I'd like to put it into an array and simplify and shorten my code, that is where I get lost. I stripped down the code to show what I currently have going on. Any ideas?</p> <p>.h</p> <pre><code>@interface { FMOD::Sound *sound1; FMOD::Sound *sound2; FMOD::Sound *sound3; FMOD::Sound *sound4; FMOD::Sound *sound5; FMOD::Sound *sound6; FMOD::Sound *sound7; FMOD::Sound *sound8; FMOD::Sound *sound9; } - (IBAction)playSound1:(id)sender; - (IBAction)stopSound1:(id)sender; - (IBAction)playSound2:(id)sender; - (IBAction)stopSound2:(id)sender; - (IBAction)playSound3:(id)sender; - (IBAction)stopSound3:(id)sender; - (IBAction)playSound4:(id)sender; - (IBAction)stopSound4:(id)sender; - (IBAction)playSound5:(id)sender; - (IBAction)stopSound5:(id)sender; - (IBAction)playSound6:(id)sender; - (IBAction)stopSound6:(id)sender; - (IBAction)playSound7:(id)sender; - (IBAction)stopSound7:(id)sender; - (IBAction)playSound8:(id)sender; - (IBAction)stopSound8:(id)sender; - (IBAction)playSound9:(id)sender; - (IBAction)stopSound9:(id)sender; </code></pre> <p>m.</p> <pre><code>- (void)viewWillAppear:(BOOL)animated { [[NSString stringWithFormat:@"%@/sound1.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound1); ERRCHECK(result); [[NSString stringWithFormat:@"%@/sound2.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound2); ERRCHECK(result); [[NSString stringWithFormat:@"%@/sound3.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound3); ERRCHECK(result); [[NSString stringWithFormat:@"%@/sound4.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE, NULL, &amp;sound4); ERRCHECK(result); result = sound4-&gt;setMode(FMOD_LOOP_NORMAL); ERRCHECK(result); [[NSString stringWithFormat:@"%@/sound5.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound5); ERRCHECK(result); [[NSString stringWithFormat:@"%@/sound6.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound6); ERRCHECK(result); [[NSString stringWithFormat:@"%@/sound7.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound7); ERRCHECK(result); [[NSString stringWithFormat:@"%@/sound8.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound8); ERRCHECK(result); [[NSString stringWithFormat:@"%@/sound9.wav", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding]; result = system-&gt;createSound(buffer, FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &amp;sound9); ERRCHECK(result); } - (IBAction)playSound1:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound1, false, &amp;wob01); ERRCHECK(result); } - (IBAction)stopSound1:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob01-&gt;stop(); ERRCHECK(result); } - (IBAction)playSound2:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound2, false, &amp;wob02); ERRCHECK(result); } - (IBAction)stopSound2:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob02-&gt;stop(); ERRCHECK(result); } - (IBAction)playSound3:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound3, false, &amp;wob03); ERRCHECK(result); } - (IBAction)stopSound3:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob03-&gt;stop(); ERRCHECK(result); } - (IBAction)playSound4:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound4, false, &amp;wob04); ERRCHECK(result); } - (IBAction)stopSound4:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob04-&gt;stop(); ERRCHECK(result); } - (IBAction)playSound5:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound5, false, &amp;wob05); ERRCHECK(result); } - (IBAction)stopSound5:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob05-&gt;stop(); ERRCHECK(result); } - (IBAction)playSound6:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound6, false, &amp;wob06); ERRCHECK(result); } - (IBAction)stopSound6:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob06-&gt;stop(); ERRCHECK(result); } - (IBAction)playSound7:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound7, false, &amp;wob07); ERRCHECK(result); } - (IBAction)stopSound7:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob07-&gt;stop(); ERRCHECK(result); } - (IBAction)playSound8:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound8, false, &amp;wob08); ERRCHECK(result); } - (IBAction)stopSound8:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob08-&gt;stop(); ERRCHECK(result); } - (IBAction)playSound9:(id)sender { FMOD_RESULT result = FMOD_OK; result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound9, false, &amp;wob09); ERRCHECK(result); } - (IBAction)stopSound9:(id)sender { FMOD_RESULT result = FMOD_OK; result = wob09-&gt;stop(); ERRCHECK(result); } </code></pre> <p>As you can see, all the code is just repeated. This is the only way I've been able to get it to work but I know that these can be put into an array, I just can't figure it out. Possibly an NSMutableArray and list out "sound1", "sound2", etc etc.. then assign each button a tag in interface builder? Ideally, I'd like to have one function for stopSound, one for playSound, etc. that uses a tag to play or stop the correct sound file. When using FMOD's system->createSound(), the last argument is a variable to store the newly created sound in. Is there any way to store it in an array or dictionary instead? If so I can't figure it out.</p> <p>Any advice would be MORE than appreciated. I'd love to stop beating my head against this simple issue.</p> <p>Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload