Ios 为什么我在Xcode中遇到nsrange异常

Ios 为什么我在Xcode中遇到nsrange异常,ios,xcode,Ios,Xcode,我正在尝试从我的项目中加载plist,在我意外删除plist之前,它一直在工作。plist有5个阵列,每个阵列有2个元素。我知道程序试图访问超出数组范围的内容,但我不知道该索引设置在哪里?这是它轰炸的代码:这段代码成功执行了两次,然后出于某种原因,它尝试第三次访问它并轰炸第一行,为什么 它抛出以下异常: NSRangeException -[_NSCFARRAY objectAtIndex] index(2) beyond bounds (2) 请帮忙,这是周一到期的最后一个项目,现在我觉得我

我正在尝试从我的项目中加载plist,在我意外删除plist之前,它一直在工作。plist有5个阵列,每个阵列有2个元素。我知道程序试图访问超出数组范围的内容,但我不知道该索引设置在哪里?这是它轰炸的代码:这段代码成功执行了两次,然后出于某种原因,它尝试第三次访问它并轰炸第一行,为什么

它抛出以下异常:

NSRangeException -[_NSCFARRAY objectAtIndex] index(2) beyond bounds (2)
请帮忙,这是周一到期的最后一个项目,现在我觉得我必须重新开始

 NSString *nameOfAccount = [account objectAtIndex:indexPath.row];
 cell.textLabel.text = nameOfAccount;
 NSString *accountNumber = [number objectAtIndex:indexPath.row];
 cell.detailTextLabel.text = accountNumber;

由于在同一单元格中显示数据,因此可以将帐户的名称和编号都包含到字典或自定义模型对象中,该对象将保存这两个信息

在plist中,这可能是字典对象的结构、数组

当您显示信息时。为数据源创建一个数组,比如说
accounts

#define kAccountName @"Name"
#define kAccountNumber @"Number"

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Accounts" ofType:@"plist"];
    self.accounts = [NSArray arrayWithContentsOfFile:filePath];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.accounts count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *account = self.accounts[indexPath.row];

    cell.textLabel.text = account[kAccountName];
    cell.detailTextLabel.text = account[kAccountNumber];

    // Configure the cell...

    return cell;
}

因为您使用两个数组来获取name和account,同时给出行数,所以您可能已经给出了其中任何一个的计数。如果他们的计数有任何可能不同,则可能发生错误。如果可以共享plist的结构,则可以修改代码以删除此错误。显示“
表视图:numberofrowsinssection:
”数据源方法;谢谢,我同意你的观点,但奇怪的是,我在每个数组中添加了更多元素,现在它又开始工作了。我不明白这有什么关系。现在,5个数组中的每一个都有相同的5个对应元素。很高兴知道它在工作,但在处理数据源的方式上存在一些问题。为什么不使用单个字典数组呢。因为在tableView:numberOfRowsInSection:method中,在返回之前必须首先比较所有数组,以获得相同数量的对象。