SIGABRT使用indexPath.row访问NSArray

时间:2013-07-31 18:39:14

标签: ios objective-c cocoa-touch nsdictionary sigabrt

我正在开发一个游戏服务器公司的应用程序,部分应用程序要求用户查看他或她的游戏服务器列表以及他们是否在线,离线,他们有多少玩家,服务器这个数据都可以在从MySQL数据库更新的Web文件中找到的PHP文件中找到,在查看时输出JSON。

使用下面的代码,这似乎不起作用。我加载视图并立即在NSDictionary *myServer = [servers objectAtIndex:indexPath.row];的行上获得“Thread 1:signal SIGABRT”错误。删除indexPath.row并将其替换为0或1时,数据将显示在我的Storyboard中的UITableView上,除了它连续显示4次且仅针对JSON文件中的该条目(0或1)。我不能将它保持在固定的数字,因为客户端可能有100台服务器,或者只有5台服务器,这就是为什么我需要像indexPath.row这样的东西。下面,我还准确地附上了从服务器给出并直接从应用程序代码访问的JSON的样子

我真的很感激,如果有人可以请让我知道问题是什么,并提出一个独特的解决方案来摆脱这个SIGABRT错误,一旦我们这样做,确保它没有显示4次TableView就像它现在一样。

我的标题文件:

#import <UIKit/UIKit.h>
#import "ServerDetailViewController.h"

@interface SecondViewController : UITableViewController {
    IBOutlet UITableView *mainTableView;

    NSDictionary *news;
    NSMutableData *data;
}

@property (weak, nonatomic) IBOutlet UIBarButtonItem *refreshServersButton;

- (IBAction)refreshServers:(id)sender;

@end

我的主要档案:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:@"REDACTED"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
    [mainTableView reloadData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to load server list. Make sure you are connect to either 3G or Wi-Fi or try again later." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

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

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [news count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UIColor *colorGreen = [UIColor colorWithRed:91.0f/255.0f green:170.0f/255.0f blue:101.0f/255.0f alpha:1.0f];
    UIColor *colorRed = [UIColor redColor];

    static NSString *CellIdentifier = @"MainCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UILabel *serverName = (UILabel *)[cell viewWithTag:100];
    UILabel *serverPlayers = (UILabel *)[cell viewWithTag:101];
    UILabel *serverStatus = (UILabel *)[cell viewWithTag:102];
    UILabel *serverOfflineName = (UILabel *)[cell viewWithTag:103];

    serverPlayers.textColor = [UIColor grayColor];

    NSDictionary *resultDict = [news objectForKey:@"result"];
    NSArray *servers = [resultDict objectForKey:@"servers"];
    NSDictionary *myServer = [servers objectAtIndex:indexPath.row];

    NSString *titleOfServer = [myServer objectForKey:@"title"];
    NSNumber *statusOfServer = [NSNumber numberWithInt:[[myServer objectForKey:@"status"] intValue]];
    NSNumber *playersOnServer = [NSNumber numberWithInt:[[myServer objectForKey:@"players"] intValue]];

  if ([[statusOfServer stringValue] isEqualToString:@"0"]) {

      serverName.text = @"";
      serverOfflineName.text = titleOfServer;
      serverStatus.textColor = colorRed;
      serverStatus.text = @"OFFLINE";
      serverPlayers.text = @"";
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

  } else if ([[statusOfServer stringValue] isEqualToString:@"1"]) {

      serverName.text = titleOfServer;
      serverOfflineName.text = @"";
      serverStatus.textColor = colorGreen;
      serverStatus.text = @"ONLINE";
      serverPlayers.text = [playersOnServer stringValue];
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

  } else if ([[statusOfServer stringValue] isEqualToString:@"2"]) {

      serverName.text = @"";
      serverOfflineName.text = titleOfServer;
      serverStatus.textColor = [UIColor blueColor];
      serverStatus.text = @"BUSY";
      serverPlayers.text = @"";
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


  } else if ([[statusOfServer stringValue] isEqualToString:@"3"]) {

      serverName.text = @"";
      serverOfflineName.text = titleOfServer;
      serverStatus.textColor = [UIColor grayColor];
      serverStatus.text = @"SUSPENDED";
      serverPlayers.text = @"";
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


  } else if ([[statusOfServer stringValue] isEqualToString:@"-1"]) {

      serverName.text = @"";
      serverOfflineName.text = titleOfServer;
      serverStatus.textColor = [UIColor orangeColor];
      serverStatus.text = @"CRITICAL ERROR";
      serverPlayers.text = @"";
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


  }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ServerDetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
    [self.navigationController pushViewController:detail animated:YES];
}

- (IBAction)refreshServers:(id)sender {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:@"REDACTED"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

@end

来自服务器的JSON代码:{"status":"OK","error":"","debug":"2 server(s)","result":{"servers":[{"id":1,"title":"Test","players":0,"slots":10,"status":3},{"id":2,"title":"Creative Spawn","players":0,"slots":5,"status":-1}]}}

2 个答案:

答案 0 :(得分:2)

从您的代码中,这看起来像是错误的来源。(但是,我没有阅读整个内容。)

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [news count]; //counted number of items in your whole json object.
}

并在您的cellForRowAtIndexPath:(NSIndexPath *)indexPath

NSDictionary *resultDict = [news objectForKey:@"result"];
NSArray *servers = [resultDict objectForKey:@"servers"];
// you used a different array(an item of the whole json array).
// Since news object has more items than servers, it caused an out of bound here. 
NSDictionary *myServer = [servers objectAtIndex:indexPath.row];

尝试对您的代码执行以下操作

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *resultDict = [news objectForKey:@"result"];
    NSArray *servers = [resultDict objectForKey:@"servers"];
    return [servers count]; //counted number of items in your whole json object.
}

答案 1 :(得分:2)

TL; DR
崩溃的原因是,您使用news.count作为表中的行数,但引用了servers数组中的indexPath.row(不保证是这样)。

这里有一些事情:

首先,这不是一个非常熟练的网络方式,因为你支持iOS5,我建议使用以下方法(或类似的东西):

[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {  
    NSString *dataString = [[NSString alloc] initWithBytes:[data bytes] length:[[data bytes] length] encoding:NSUTF8StringEncoding];  
}];

其次,我强烈推荐MVC model,管理数据不是控制器的东西 - 正如有人提到的那样,目前的代码并不像它可能那么容易阅读(以及维护它!)。

第三,我建议你采用更多的防御性编码,以下是我在阅读时发现的要点:

  1. 新闻,而不是[NSJSONSerialization JSONObjectWithData:data options:nil error:nil];,并不保证根本不会返回字典;虽然它被视为这样
  2. 崩溃的原因是,您使用news.count作为表中的行数,但引用了servers数组中的indexPath.row(不保证是这样)。
  3. 如果我是你,我可能会先简化网络,然后创建一个简单的模型(例如Server),让模型解析与之相关的JSON。我甚至在你的服务器模型中包含一个静态方法,比如'retrieveServers',它返回一个服务器对象的NSArray。

    这样,你的控制器所做的就是:

    [self setNews:[Server retrieveServers]];
    [_tableView reloadData];
    

    而不是在控制器中包含大量不相关的代码 - 这将提高可维护性和可读性。

    如果您如此倾向,可以采取另一步,提供自定义访问者,而不是直接通过模型引用成员,例如:

    Server *currentServer = nil;
    if( self.news.count > indexPath.row ) {
       currentServer = [_news objectAtIndex:indexPath.row];
    }
    
    [serverPlayers setText:(currentServer ? [currentServer getPlayers] : [Server defaultPlayerValue]]
    

    上面的代码是安全的,检查数组中是否至少包含相同数量的元素,其次,在将值分配给表格单元格时(可能会重复使用,因此需要)为每个可能的执行分支设置合理的值)。执行上述操作的优点:可读性,集中默认值,可维护性。

    我道歉,如果这看起来相当矫枉过正(TL; DR补充; /),尝试提供一些指示,如果您没有其他理由采用上述提示,这将有助于调试。