0

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?

6
  • You didn't alloc and init the NSMutableArray before send it addObject: message, do you ? try this :soundsArray = [NSMutableArray array]; Commented Dec 26, 2014 at 5:30
  • @KudoCC I added it as a property in the header and synthesized it, I figured that would initialize it no? Commented Dec 26, 2014 at 5:32
  • 1
    You must initialize it, "add it as a property and synthesize it" can't initialize it. Commented Dec 26, 2014 at 5:35
  • @KudoCC Really? That's interesting... I figured that would be automatically taken care of in the synthesized setter method. Odd. Oh well. Thank you for your help! If you can add this as an answer I'll gladly accept. Commented Dec 26, 2014 at 5:46
  • 1
    In synthesized setter method, it will take care of it but you didn't call its setter method. In order to trigger its setter method, you must create a NSMutableArray first.NSMutableArray *mArray = [NSMutableArray array] ; self.soundsArray = mArray ; Commented Dec 26, 2014 at 5:51

1 Answer 1

2

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);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir. Figured the alloc and initalization would have been taken care of in the setter method of the synthesis. Guess not. Thank you for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.