gpt4 book ai didi

ios - SIGABRT 使用 indexPath.row 访问 NSArray

转载 作者:行者123 更新时间:2023-12-01 18:20:05 25 4
gpt4 key购买 nike

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

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

如果有人可以让我知道问题是什么并提出一个针对我的情况的独特解决方案来摆脱这个 SIGABRT 错误,我将非常感激,一旦我们这样做,确保它不会在 TableView 中显示 4 次就是现在。

我的头文件:

#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}]}}

最佳答案

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

- (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.
}

关于ios - SIGABRT 使用 indexPath.row 访问 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17978287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com