数组不为空但仍然得到错误索引0的界限

时间:2015-05-11 14:21:07

标签: ios objective-c arrays

我正在使用SQLite,我想保存名称,地址和电话文本字段,以便在下一个视图控制器中显示"显示详细信息"在第一个VC中单击按钮。

我放置"保存"和"显示详细信息"第一个VC中的按钮,以及"之前的#34;和" next"第二个VC中的按钮。每当我点击"显示详细信息"我收到此错误消息:

  

索引0超出空数组的边界。

但是,我看到数组不是空的。我想将学生的详细信息存储在数组中。

- (void)viewDidLoad
{
    [super viewDidLoad];



    NSString *homeDirectory = NSHomeDirectory();
    NSString *documentsDirectoryPath = [homeDirectory stringByAppendingPathComponent:@"Documents"];
    self.dbFilePathInDocuments = [documentsDirectoryPath stringByAppendingPathComponent:@"details.db"];
    self.studentDetails = [[NSMutableArray alloc]init];

    NSString *selectQuery = [NSString stringWithFormat:@"select name,address,phone from contacts"];
    sqlite3_open([self.dbFilePathInDocuments UTF8String], &dataBase);
    sqlite3_prepare_v2(dataBase, [selectQuery UTF8String], -1,&selectStatement, NULL);
    while (sqlite3_step(selectStatement) == SQLITE_ROW)
    {
        NSMutableDictionary *studentDict = [[NSMutableDictionary alloc]init];
        NSString *name = [NSString stringWithFormat:@"%s",sqlite3_column_text(selectStatement, 0)];
        NSString *address = [NSString stringWithFormat:@"%s",sqlite3_column_text(selectStatement, 1)];
        NSString *phone = [NSString stringWithFormat:@"%s",sqlite3_column_text(selectStatement, 2)];
        [studentDict setObject:name forKey:@"name"];
        [studentDict setObject:address forKey:@"address"];
        [studentDict setObject:phone forKey:@"phone"];
        [self.studentDetails addObject:studentDict];
        NSLog(@"student is:%@",self.studentDetails);
    }

    sqlite3_finalize(selectStatement);
    sqlite3_close(dataBase);

    self.nameLabel.text = [[self.studentDetails objectAtIndex:0] valueForKey:@"name"];
    self.addressLabel.text = [[self.studentDetails objectAtIndex:0] valueForKey:@"address"];
    self.phoneLabel.text = [[self.studentDetails objectAtIndex:0] valueForKey:@"phone"];

    currentStudentIndex = 0;

}

- (IBAction)clickPrevious:(id)sender {
    if(currentStudentIndex <=0)
    {
        currentStudentIndex = 0;
    }else
    {
        currentStudentIndex = currentStudentIndex - 1;
    }


    self.nameLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:@"name"];
    self.addressLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:@"address"];
    self.phoneLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:@"phone"];
}


- (IBAction)clickNext:(id)sender {

    if(currentStudentIndex >= [self.studentDetails count] - 1)
    {
        currentStudentIndex = [self.studentDetails count] - 1;
    }else
    {
        currentStudentIndex = currentStudentIndex + 1;
    }

    self.nameLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:@"name"];
    self.addressLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:@"address"];
    self.phoneLabel.text = [[self.studentDetails objectAtIndex:currentStudentIndex] valueForKey:@"phone"];
}

0 个答案:

没有答案