NSMutableArray有错误的空数组

时间:2013-06-21 09:42:38

标签: xcode json uitableview nsmutablearray

我对NSMutablearray和JSON解析有一些问题。 那我在做什么?来自JSON的make解析并发送到我的TableViewCell。 我有我的代码:

H:

{
    NSMutableData *webdata;

    NSURLConnection *connection;

    NSMutableArray *array;

    NSMutableArray *array2;

NSTimer *timer;
}

米:

{
[super ViewDidload]

    [[self tableTrack] setDelegate:self];
    [[self tableTrack] setDataSource:self];
    array = [[NSMutableArray alloc] init];
    array2 = [[NSMutableArray alloc] init];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(plistGet) userInfo:nil repeats:YES];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webdata setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webdata appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error load");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
    NSDictionary *playlist =[allDataDictionary objectForKey:@"playlist"];

    for (NSDictionary *diction in playlist) {
        NSDictionary *artist = [diction objectForKey:@"artist"];
        NSDictionary *song = [diction objectForKey:@"song"];
        NSString *name = [artist objectForKey:@"name"];
        NSString *namesong = [song objectForKey:@"name"];
        [array addObject:name];
        [array2 addObject:namesong];
    }
    [[self tableTrack]reloadData];
}

-(void)plistGet {


    [array removeAllObjects];
    [array2 removeAllObjects];
    NSURL *url = [NSURL URLWithString:@"http://plist.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        webdata = [[NSMutableData alloc]init];
    }

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
//    return [array2 count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(! cell)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
    }

    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];

    cell.detailTextLabel.text = [array objectAtIndex:indexPath.row];

    cell.textLabel.textColor = [UIColor colorWithRed:(50/255.0) green:(200/255.0) blue:(255/255.0) alpha:1];


    [tableTrack setBackgroundView:nil];

    tableTrack.backgroundColor = [UIColor clearColor];

   cell.detailTextLabel.font=[UIFont fontWithName: @"Helvetica" size:11];

    UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 9.0 ];
    cell.textLabel.font  = myFont;

    self.tableTrack.separatorStyle = UITableViewCellSeparatorStyleNone;

   UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
    separatorView.layer.borderColor = [UIColor darkGrayColor].CGColor;
    separatorView.layer.borderWidth = 0.5;
    [cell.contentView addSubview:separatorView];

    return cell;

}

所有工作都很好但是在5或3分钟后我出错并且我的应用程序崩溃了。

错误:

2013-06-21 12:32:41.502 EuropaPlusUA [651:707] *由于未捕获的异常'NSRangeException'而终止应用,原因:'* - [__ NSArrayM objectAtIndex:]:index 0超出空数组的边界' * 第一次抛出调用堆栈: (0x353f188f 0x37798259 0x3533a9db 0xfa9d5 0x32e85efb 0x32e84fd9 0x32e84763 0x32e28f37 0x353501fb 0x32220aa5 0x322206bd 0x32224843 0x3222457f 0x3221c4b9 0x353c5b1b 0x353c3d57 0x353c40b1 0x353474a5 0x3534736d 0x36fe3439 0x32e53cd5 0xf8843 0xf87d0) 终止调用抛出异常

这是什么?请帮助。

3 个答案:

答案 0 :(得分:1)

你的问题是你可能在3-5分钟后再做一次plistget方法。该方法将丢弃数组中的所有对象。在数据加载期间以及清除数组的时间内,表数据源为空,但是您尝试从索引0获取此对象。这就是崩溃发生的原因。

然而,解决方案很简单。根本不要调用removeAllObjects方法。 只需将数组替换为您将检索的内容即可。

替换从服务器获取数据时需要执行的操作:

NSMutableArray *tempDataArray = [[NSMutableArray alloc] init];
//put retrieved data from your web call in the tempDataArray
array = tempDataArray;

答案 1 :(得分:0)

我认为唯一的objectAtIndex就在这里。

cell.detailTextLabel.text = [array objectAtIndex:indexPath.row];

似乎很可能数组被plistget清空了

[array removeAllObjects];

答案 2 :(得分:0)

Totumus Maximus是对的。

如果您只是需要缩小清除阵列和重新加载数据之间的差距,为什么不尝试不删除它们直到新数据准备好?然后只需替换它们

或者首先复制它们(比如backUpArray和backUpArray2),然后在尝试加载之前检查数组/ array2中是否有数据(类似if([array count])if (array) {...)细胞。

如果数组中没有任何内容,请使用[backUpArray lastObject]为(简短?)时间提供一些数据,直到重新加载数组。