ios - 获取带有通知的数组

标签 ios objective-c

我试图在通知可用时将其放入数组中,但当我推送新通知时,数组的计数重置为 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]);
    }

最佳答案

每次收到通知时,您似乎都在初始化变量 rlistMsgReceived(尽管很难从您提供的上下文中判断)。

你不应该那样做,因为每次你都会得到一个数组,你会在其中插入一个对象 - 因此每次通知后计数将为 1。

您可以尝试将数组初始化移到您的方法之外;将其声明为类的属性并在初始化程序中对其进行初始化。

关于ios - 获取带有通知的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24896015/

相关文章:

iphone - 如何在 UIBezierPath 上获取边框

iphone - UITableview背景 View 颜色-iphone

ios - 仅当 Deployment target 大于 6.0 时如何使用 AdMob?

ios - 根据选择状态更改 Uibutton 字体大小

ios - 进入Suspended状态后会调用AppDelegate的什么方法?

ios - GTMOAuth2Authentication 存储访问 token 时出现问题

ios - 如何在 objective-c 中打假电话

objective-c - 如何在不阻塞界面的情况下为 NSTableView 加载数据?

objective-c - 无法使用 Swift 将此 CGFloat 分配给 XCode 中的属性

iOS Game Center GKScore上下文,如何编码信息?