Henry F
Henry F

Reputation: 4980

Simply Adding Strings To a Mutable Array

I'm trying to do something which is seemingly extremely trivial, but am running into some odd issues. I'm trying to create strings that contain paths to objects in the main bundle, then store the string in an array, like so:

-(void)loadSounds {

    NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"Sound1" ofType:@"wav"];
    [soundsArray addObject:soundPath1];

    NSString *soundPath2 = [[NSBundle mainBundle] pathForResource:@"Sound2" ofType:@"wav"];
    [soundsArray addObject:soundPath2];

    NSString *soundPath3 = [[NSBundle mainBundle] pathForResource:@"Sound3" ofType:@"Wav"];
    [soundsArray addObject:soundPath3];

    NSLog(@"In the model, soundsArray is: %lu", (unsigned long)soundsArray.count);

Now, for some reason, the NSLog reads that the array is null... even though I'm adding the objects. I figured that is a simple straightforward was to add a string to an array, but apparently not. I must be missing something very simple. Does anyone have an idea? As a side note, logic such as loading sounds into the app should be loaded by the Model, and controlled by the controller according to the MVC design pattern, correct?

Upvotes: 0

Views: 70

Answers (1)

user4238267
user4238267

Reputation:

let try this

 -(void)loadSounds {


    soundsArray=[NSMutableArray alloc]init];

        NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"Sound1" ofType:@"wav"];
        [soundsArray addObject:soundPath1];

        NSString *soundPath2 = [[NSBundle mainBundle] pathForResource:@"Sound2" ofType:@"wav"];
        [soundsArray addObject:soundPath2];

        NSString *soundPath3 = [[NSBundle mainBundle] pathForResource:@"Sound3" ofType:@"Wav"];
        [soundsArray addObject:soundPath3];

        NSLog(@"In the model, soundsArray is: %lu", (unsigned long)soundsArray.count);
    }

Upvotes: 2

Related Questions