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