如何在我的场景中纠正此错误_NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 7]'

Posted

技术标签:

【中文标题】如何在我的场景中纠正此错误_NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 7]\'【英文标题】:How to rectify this error in my scenario _NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 7]'如何在我的场景中纠正此错误_NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 7]' 【发布时间】:2015-11-30 07:43:38 【问题描述】:

在这个屏幕截图中,我有两个下拉问题和项目。如果我滚动问题下拉它会显示错误 _NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 7]'。尽管我动态地获取值。

我对项目下拉列表的 json 响应如下:

 
     "projects": [
      
      "id": 8,
      "name": "Andriod APP",
      "identifier": "andriod-app",
      "description": "",
      "status": 1,
      "is_public": true,
      "created_on": "2015-06-29T11:54:23Z",
      "updated_on": "2015-06-29T11:54:23Z"
       ,
       ],
      "total_count": 8,
      "offset": 0,
      "limit": 25
      

my json response like this for issues drpdown
    
  "issues": [
    
      "id": 22,
      "project": 
        "id": 4,
        "name": "Vitals"
      ,
      "tracker": 
        "id": 3,
        "name": "Support"
      ,
      "status": 
        "id": 1,
        "name": "New"
      ,
      "priority": 
        "id": 2,
        "name": "Normal"
      ,
      "author": 
        "id": 1,
        "name": "Redmine Admin"
      ,
      "subject": "Lead page creation",
      "description": "",
      "start_date": "2015-06-25",
      "done_ratio": 0,
      "created_on": "2015-06-25T10:22:22Z",
      "updated_on": "2015-06-25T10:22:22Z"
    ,
    ],
  "total_count": 17,
  "offset": 0,
  "limit": 25
 

我的一段代码:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *btnoutlet;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSArray * data;
- (IBAction)btnAction:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *btnoutlet1;
@property (weak, nonatomic) IBOutlet UITableView *tableView1;
@property (strong,nonatomic) NSArray * data1;
- (IBAction)btnAction1:(id)sender;
@end

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad 
[super viewDidLoad];
// to fetch and parse json response of projects dropdown
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURLWithString:@"http:xxx/projects.json"]];
NSError * error = nil;
NSURLResponse * response = nil;
NSData * jsonSource = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response error:&error];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
if ([jsonObjects objectForKey:@"projects"] != [NSNull null]) 
self.data = [jsonObjects  objectForKey:@"projects"];
NSLog(@" %@",self.data);

self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView =[UIView  new];
//  to fetch and parse the json response of issues dropdown
NSURLRequest * urlRequest1 = [NSURLRequest requestWithURL:       [NSURLURLWithString:@"http:xxx/redmine/issues.json"]];
NSError * error1 = nil;
NSURLResponse * response1 = nil;
NSData * jsonSource1 = [NSURLConnection  sendSynchronousRequest:urlRequest1returningResponse:&response1error:&error1];
id jsonObjects1 = [NSJSONSerialization JSONObjectWithData:jsonSource1  options:NSJSONReadingMutableContainers error:nil];
if ([jsonObjects1 objectForKey:@"issues"] != [NSNull null]) 
self.data1 = [jsonObjects1 objectForKey:@"issues"];
NSLog(@" %@",self.data1);

self.tableView1.delegate = self;
self.tableView1.dataSource = self;
self.tableView1.tableFooterView =[UIView  new];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

if(tableView == self.tableView)

return [self.data count];

if(tableView == self.tableView1)

return [self.data1 count];

return 0;

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

static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSDictionary * tmpdict = [self.data objectAtIndex:indexPath.row];
NSLog(@ " %@",tmpdict);
NSDictionary * tmpdict2 = [self.data1 objectAtIndex:indexPath.row];
NSLog(@ " %@",tmpdict2);
if (cell == nil) 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

if(tableView == self.tableView)
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [tmpdict objectForKey:@"name"];
NSLog(@ " %@",cell.textLabel.text);

else if(tableView == self.tableView1) 
cell.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [tmpdict2 objectForKey:@"subject"];
NSLog(@ " %@",cell.textLabel.text);

// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

if (tableView==self.tableView) 
UITableViewCell * Cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self.btnoutlet setTitle:Cell.textLabel.text forState:UIControlStateNormal];
self.tableView.hidden = YES;

if (tableView==self.tableView1) 
UITableViewCell * Cell = [self.tableView1 cellForRowAtIndexPath:indexPath];
[self.btnoutlet1 setTitle:Cell.textLabel.text forState:UIControlStateNormal];
self.tableView1.hidden = YES;


错误可能在哪里?在此代码中或需要进行任何修改。请提出一些想法。

【问题讨论】:

执行此操作:NSDictionary * tmpdict = [self.data objectAtIndex:indexPath.row]; NSLog(@ " %@",tmpdict); NSDictionary * tmpdict2 = [self.data1 objectAtIndex:indexPath.row]; NSLog(@ " %@",tmpdict2); 仅在测试中和 if(tableView == self.tableView 或 self.tableView1)的正确之一中。 感谢您的回答 【参考方案1】:

你需要输入:

NSDictionary * tmpdict = [self.data objectAtIndex:indexPath.row];
NSLog(@ " %@",tmpdict);
NSDictionary * tmpdict2 = [self.data1 objectAtIndex:indexPath.row];
NSLog(@ " %@",tmpdict2);

在适当的if 条件内。在当前情况下,如果 self.dataself.data1 计数不同,则会崩溃。

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

   static NSString *simpleTableIdentifier = @"SimpleTableItem";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
   if (cell == nil)
   
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
   

   if(tableView == self.tableView)
   
      NSDictionary * tmpdict = [self.data objectAtIndex:indexPath.row];
      NSLog(@ " %@",tmpdict);
      cell.textLabel.font = [UIFont systemFontOfSize:12];
      cell.backgroundColor = [UIColor greenColor];
      cell.textLabel.text = [tmpdict objectForKey:@"name"];
      NSLog(@ " %@",cell.textLabel.text);
   
   else if(tableView == self.tableView1)
   
      NSDictionary * tmpdict2 = [self.data1 objectAtIndex:indexPath.row];
      NSLog(@ " %@",tmpdict2);
      cell.backgroundColor = [UIColor greenColor];
      cell.textLabel.text = [tmpdict2 objectForKey:@"subject"];
      NSLog(@ " %@",cell.textLabel.text);
   
   // cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
     return cell;

【讨论】:

@downvoter ....我希望只添加图像....如果您获得 10 个声誉,那么只有....它显示警报 thst 这就是我发布这样的图像的原因.... 是的,兄弟是否可以将主题和 id 组合在单个文本标签中。有可能吗? @catherine007: 可以,可以使用 nsstring 的 stringWithFormat: 方法来实现

以上是关于如何在我的场景中纠正此错误_NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 7]'的主要内容,如果未能解决你的问题,请参考以下文章

如何从我的数组列表中删除 __ob__: Observer?

Firebase Swift 无法将“__NSArrayM”(0x10591cc30)类型的值转换为“NSDictionary”

Swift JSON 错误,无法将类型“__NSArrayM”(0x507b58)的值转换为“NSDictionary”(0x507d74)

Facebook JSON 提要 - __NSArrayM insertObject:atIndex:]:对象不能为 nil

[__NSArrayM objectAtIndex:]: 索引 9223372036854775807 超出范围 [0 .. 13]'

如何提取 __NSArrayM 中的每个元素?