user3866054
user3866054

Reputation: 1

Obtain an array with notifications

I am trying to put notifications in an array as they become available, but the count of the array is reset to 1 when I push a new notification.

This is the code:

    int r = 0;
    listMsgReceived = [[NSMutableArray alloc] init];

    if (notification)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification received" message:[NSString stringWithFormat:@"%@", message]  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
        [listMsgReceived insertObject:message atIndex:r];
        r++;
        NSLog(@"apres: %d \n", [listMsgReceived count]);
    }

Upvotes: 0

Views: 66

Answers (1)

driis
driis

Reputation: 164331

It looks like you are initializing the variables r and listMsgReceived each time your notification is received (though it's hard to tell from the context you provided).

You should not do that, because that gets you a new array each time, where you insert one object - hence the count will be one after each notification.

You could try moving your array initialization outside of your method; declare it as a property on your class and initialize it in the initializer.

Upvotes: 2

Related Questions