【问题标题】:Array lost its object after some time一段时间后数组丢失了它的对象
【发布时间】:2017-01-07 19:58:40
【问题描述】:

我正在创建聊天应用程序,我向arrayChat 添加消息:

-(void)msgRecevied:(NSMutableDictionary *)messageContent
{
    NSString *m = [messageContent objectForKey:kMsg];
    [messageContent setObject:[m substituteEmoticons] forKey:kMsg];
    self.arrayTemp = messageContent;

    [self.arrayChat addObject:self.arrayTemp];

    [_tblChat reloadData];

    if(self.arrayChat.count > 1){

        NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:self.arrayChat.count-1
                                                       inSection:0];

        [self.tblChat scrollToRowAtIndexPath:topIndexPath
                            atScrollPosition:UITableViewScrollPositionMiddle
                                    animated:YES];
    }
}

但是一段时间后,当我将设备放置一段时间后,当我发送消息后,它会将对象添加到 arrayChat,但它没有加载到 tableView 中,我重新加载表,它显示了它从 @ 丢失的最后一个对象987654324@.

我声明arrayChat 喜欢:

@interface ViewController : UIViewController
{

    NSMutableArray *arrayChat;
}
@property (strong,nonatomic) NSMutableArray *arrayChat;

我也是@synthesize arrayTemp;

这里有什么错误吗?

编辑

我将我的 arrayChat 添加到其他类,并将该类声明为 Appdelegate 我使用该类访问应用程序上的该数组,它正在工作,但是当我将对象添加到数组但它没有重新加载我的数组时出现问题表一次,我正在放大按钮上的聊天视图,显示我最后一次添加到数组的对象。

这是我放大视图时的代码:

-(IBAction)onClickViewLarge:(id)sender{

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth  = screenRect.size.width;
    CGFloat screenHeight  = screenRect.size.height;


    if (!isShow) {

        [_btnOpen setImage:[UIImage imageNamed:@"ico_expand"] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.5 animations:^{

            if(screenHeight >= 736)
                self.view.frame = CGRectMake(0,66 , screenWidth, screenHeight - 66);
            else if (screenHeight >= 667)
                self.view.frame = CGRectMake(0,60 , screenWidth, screenHeight - 60);
            else if (screenHeight >= 568)
                self.view.frame = CGRectMake(0,51 , screenWidth, screenHeight - 51);
            else if (screenHeight >= 480)
                self.view.frame = CGRectMake(0,43 , screenWidth, screenHeight - 43);
        }];
        isShow = true;

    } else {
        [_btnOpen setImage:[UIImage imageNamed:@"ico_open"] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.5 animations:^{
            self.view.frame = CGRectMake(screenWidth - 250,screenHeight - 450 , 250, 450);
        }];
        isShow = false;
    }

    if([app.Glb.arrayChat count] > 0)
    {
        [self.tblChat reloadData];

        NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:app.Glb.arrayChat.count-1
                                                       inSection:0];

        [self.tblChat scrollToRowAtIndexPath:topIndexPath
                            atScrollPosition:UITableViewScrollPositionMiddle
                                    animated:YES];
    }

}

这是怎么解决的?

【问题讨论】:

  • 我认为问题出在全局 arrayTemp 对象上。尝试在你的 msgRecevied 方法中声明它
  • 没有任何影响。
  • 你的arrayTemp也强吗?如果不设置为强。我认为一段时间后它的设置为零。
  • 是的,它的 :strong,nonatomic
  • 你初始化你的self.arrayChat了吗?

标签: ios objective-c nsmutablearray xmpp openfire


【解决方案1】:

我认为问题出在您全局声明的 arrayTemp 对象上。每次您覆盖它的值时,您都会覆盖 arrayChat 本身的内容。尝试像这样将其声明为您的方法的本地

-(void)msgRecevied:(NSMutableDictionary *)messageContent
{
    NSString *m = [messageContent objectForKey:kMsg];
    [messageContent setObject:[m substituteEmoticons] forKey:kMsg];
    NSMutableDictionary *arrayTemp = [messageContent mutableCopy];

    [self.arrayChat addObject:arrayTemp];

    [_tblChat reloadData];

    if(self.arrayChat.count > 1){

        NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:self.arrayChat.count-1
                                                       inSection:0];

        [self.tblChat scrollToRowAtIndexPath:topIndexPath
                            atScrollPosition:UITableViewScrollPositionMiddle
                                    animated:YES];
    }
}

【讨论】:

  • 我的代码工作了一段时间,但是当我在应用程序中执行其他操作时,它在一段时间后丢失了,,,,
  • 您只获取数组中的最后一个对象一次或 n 次?
  • 为了arrayChat的线程安全需要做些什么吗?
  • 我将我的数组声明给其他类并使用它...它可以工作,但我必须重新加载表两次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
相关资源
最近更新 更多