Ios 获取带有通知的数组
我试图在通知可用时将其放入数组中,但在推送新通知时,数组的计数重置为1 代码如下:Ios 获取带有通知的数组,ios,objective-c,Ios,Objective C,我试图在通知可用时将其放入数组中,但在推送新通知时,数组的计数重置为1 代码如下: int r = 0; listMsgReceived = [[NSMutableArray alloc] init]; if (notification) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification received" message:[NSString
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
(尽管从您提供的上下文很难判断)
不应该这样做,因为每次插入一个对象时都会得到一个新数组,因此每次通知后的计数都是一
您可以尝试将数组初始化移到方法之外;将其声明为类上的属性,并在初始值设定项中对其进行初始化。看起来您可能正在为
r
使用一个局部变量,该变量在每次通知之前都被设置为0,但使用有限的代码很难确定。是的,您是对的。谢谢是的,你说得对。非常感谢你的回答,并抽出时间回答我的问题。祝您今天过得愉快!