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

Posted

技术标签:

【中文标题】[__NSArrayM objectAtIndex:]: 索引 9223372036854775807 超出范围 [0 .. 13]\'【英文标题】:[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 13]'[__NSArrayM objectAtIndex:]: 索引 9223372036854775807 超出范围 [0 .. 13]' 【发布时间】:2015-04-17 04:50:17 【问题描述】:

我在我的 xcode 项目中实现了一个 SQLite 数据库。我想将数据库中的所有名称查看到 tableview 中,但出现错误

NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:索引 9223372036854775807 越界 [0 .. 13]'

@interface DisplayViewController ()
@property (nonatomic, strong) DBManager *dbManager;
@property (nonatomic, strong) NSArray *arrPeopleInfo;
  @end

@implementation DisplayViewController

- (void)viewDidLoad 
    [super viewDidLoad];
    self.mytableview.delegate = self;
    self.mytableview.dataSource = self;
    
    NSString *docsDir;
    NSArray *dirPaths;
    
    
    //Get the directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];
     self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"IApp.db"];    //Build the path to keep the database
   // _databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"IApp.db"]];
   // [self getcount];
    [self loadData];
   // [self getcount];
        // Do any additional setup after loading the view.



      
- (void)loadData 
    // Form the query.
    NSString *query = @"select * from iApp";
    
   
    if (self.arrPeopleInfo != nil) 
        self.arrPeopleInfo = nil;
    
    self.arrPeopleInfo = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
    
    [self.mytableview reloadData];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return  1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return self.arrPeopleInfo.count;
    // return [arri count];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idCellRecord" forIndexPath:indexPath];
    
    NSInteger indexOfname = [self.dbManager.arrColumnNames indexOfObject:@"firstname"];
    NSInteger indexOffname = [self.dbManager.arrColumnNames indexOfObject:@"lastname"];
  
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfname], [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOffname]];
    
    
    return cell;
      

【问题讨论】:

【参考方案1】:

当您在某物中搜索某个项目时,如果该方法找不到该项目,Cocoa 将返回NSNotFound 作为索引。这也适用于-indexOfObject:NSNotFound 是一个很大的整数 (INT_MAX),您可以在 Q 的标题中看到。

因此

NSInteger indexOfname = [self.dbManager.arrColumnNames indexOfObject:@"firstname"];

如果在接收器中找不到@"firstName",则为NSNotFound。请检查您是否有这样的密钥。错字?骆驼香烟盒? firstNams?

如果您在编译时不知道密钥,则必须检查它,正如@Inder 告诉您的那样:

if(indexOfname==NSNotFound)
  // The object is not present

顺便说一句:你真的应该重新检查你的命名。它不遵循 Cocoa 命名约定。

【讨论】:

【参考方案2】:

您可以通过非常简单的检查来防止这种情况发生。

if(indexOfname < [self.dbManager.arrColumnNames count] && indexOffname < [self.dbManager.arrColumnNames count]) 
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfname], [[self.arrPeopleInfo objectAtIndex:indexPath.row] objectAtIndex:indexOffname]];     

else 
    cell.textLabel.text = @""; // or what ever you want to do

【讨论】:

【参考方案3】:

请检查indexOfnameindexOffname是否等于NSNotFound

NSInteger indexOfname = [self.dbManager.arrColumnNames indexOfObject:@"firstname"];
NSInteger indexOffname = [self.dbManager.arrColumnNames indexOfObject:@"lastname"];

if (indexOfname == NSNotFound) 
  //Then don't use this index


if (indexOffname == NSNotFound) 
  //Then don't use this index

【讨论】:

【参考方案4】:

实际上你已经成功地从Database获取了所有记录。所以左边部分是如何在UITableViewCell 中显示。根据以下代码修改cellForRowAtIndexPath

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

       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idCellRecord" forIndexPath:indexPath];


       cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [[self.arrPeopleInfo objectAtIndex:indexPath.row] valeuForKey:@"firstname"], [[self.arrPeopleInfo objectAtIndex:indexPath.row] valueForKey:@"lastname"]];


return cell;


【讨论】:

评论不用于扩展讨论;这个对话是moved to chat。

以上是关于[__NSArrayM objectAtIndex:]: 索引 9223372036854775807 超出范围 [0 .. 13]'的主要内容,如果未能解决你的问题,请参考以下文章

[__NSArrayM objectAtIndex:]: 索引 2 超出范围 [0 .. 1]'

NSRangeException',原因:'*** -[__NSArrayM objectAtIndex:]:空数组的索引 5 超出范围'

-[__NSArrayM objectAtIndex:]: 索引 11 超出界限 [0 .. 10]' 目标 c

[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]

由于未捕获的异常“NSRangeException”而终止应用程序。 [__NSArrayM objectAtIndex:]:索引 33 超出范围 [0 .. 32]'

-[__NSArrayM objectAtIndex:]:索引 0 超出了带有 userInfo 的空数组的界限(null)