Ramiz Girach
Ramiz Girach

Reputation: 169

getting value from the loop to store in array

I have this code from which i am checking songs that are present in my app and storing the number of songs in an NSMUTABLEArray but there is something wrong it always shows null

    for (int i = 1; i < 100; ++i) {
    // Load the plist
    NSString *plist = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i] ofType:@"mp3"];
    if (nil == plist) {
        NSLog(@"not found (%i)", i);
        continue;
    }




    NSLog(@"Found Songs (%i)",i);

    [MYNSMUTABLEArray addObject:[NSNumber numberWithInt:i]];

    NSLog(@"%@",MYNSMUTABLEArray);

}

the i variable is working absolutely fine

Upvotes: 0

Views: 314

Answers (1)

WDUK
WDUK

Reputation: 19030

You need to create your mutable array, so that an object exists to store the data. I presume that the code at the moment is sending the addObject: message to nil.

I'd strongly suggest a better variable name than MYNSMUTABLEArray, so here is my code snippit that should go before the loop.

NSMutableArray* myMutableArray = [NSMutableArray alloc] init];

Then where you are adding the object, you'd use:

[myMutableArray addObject:@(i)];

Bootnote: A little tip, you can use @ literals to automatically box your primitive int value into an NSNumber object. This is used in the addObject: example, and is in the form @(int).

Upvotes: 3

Related Questions