简体   繁体   English

为什么我在Xcode中得到NSRangeException

[英]why am I getting NSRangeException in Xcode

I am trying to load a plist from my project, this was working until I accidentally deleted my plist. 我正在尝试从我的项目中加载一个plist,这一直有效,直到我不小心删除了我的plist。 the plist has 5 arrays, with 2 elements apiece. plist有5个数组,每个数组2个元素。 I know that I the program is trying to access beyond the range of the array, but what I don't know is where this index is set? 我知道程序正在尝试访问数组范围之外的内容,但是我不知道该索引的位置是什么? Here is the code that it bombs on: this code is executed twice successfully,then for some reason it tries to access it a third time and bombs on the first line,why? 这是它轰炸的代码:该代码成功执行了两次,然后由于某种原因试图再次访问它,并在第一行轰炸,为什么?

It throws this exception: 它引发以下异常:

NSRangeException -[_NSCFARRAY objectAtIndex] index(2) beyond bounds (2)

please help, this is for a final project due on Monday and now I feel like I have to start over again. 请帮忙,这是定于周一完成的最终项目,现在我觉得我必须重新开始。

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

Since you are displaying the data in same cell, you can include both name and number of account into a dictionary or a custom model object which will hold both info. 由于您要在同一单元格中显示数据,因此可以将名称和客户编号同时包含在字典或自定义模型对象中,以容纳这两个信息。

In your plist this might be the structure, array of dictionary objects 在您的plist中,这可能是字典对象的结构,数组

在此处输入图片说明

When you are displaying the info. 显示信息时。 For the dataSource create an array say accounts . 为dataSource创建一个数组,说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;
}

Source code 源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我要求我的表视图滚动到顶部时,为什么会出现NSRangeException? - Why am I getting an NSRangeException when asking my table view to scroll to top? NSRangeException Xcode - NSRangeException Xcode 为什么在生成ipa时获得通用Xcode存档? - Why I am getting Generic Xcode Archive while generating ipa? 为什么我在xcode控制台中收到此CPSqliteStatementPerform错误 - Why am I getting this CPSqliteStatementPerform error in xcode console 为什么我在 Xcode 10.1 中出现 swift 编译器错误 - Why I am getting swift compiler error in Xcode 10.1 尝试运行我的应用时,我收到“由于未捕获的异常'NSRangeException'而终止”的消息 - I am getting a “Terminating due to uncaught exception 'NSRangeException'” message when trying to run my app 我在Xcode for iOS中得到了“枚举类型的隐式转换”警告,我不知道为什么 - I am getting an “Implicit conversion from enumeration type” warning in Xcode for iOS and I don't know why xcode捕获异常NSRangeException - xcode catch exception NSRangeException 为什么在xcode 7.2.1中创建项目存档时出现此链接器错误? - why am i getting this linker error while creating archive of my project in xcode 7.2.1 为什么我从Xcode收到此警告? iOS代码似乎很好 - Why am I getting this warning from Xcode? The iOS code seems fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM