0

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

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

Comments

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.