我试图在通知可用时将其放入数组中,但当我推送新通知时,数组的计数重置为 1。
这是代码:
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]);
}
最佳答案
每次收到通知时,您似乎都在初始化变量 r
和 listMsgReceived
(尽管很难从您提供的上下文中判断)。
你不应该那样做,因为每次你都会得到一个新数组,你会在其中插入一个对象 - 因此每次通知后计数将为 1。
您可以尝试将数组初始化移到您的方法之外;将其声明为类的属性并在初始化程序中对其进行初始化。
关于ios - 获取带有通知的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24896015/