Bronze Challenge Problem


#1

Hi all, i’m having a problem with the bronze challenge! i’m just getting this error

‘NSRangeException’, reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 … 2]

But well, i checked it and my code seems to be fine. I’m struggling with this challenge, can anyone help me?

the BNRItemsViewController.m

[code]#import “JDMItemsViewController.h”
#import “JDMItemStore.h”
#import “JDMItem.h”

@interface JDMItemsViewController()

@property (nonatomic) NSArray *sections;
@property (nonatomic) NSArray *headers;

@end

@implementation JDMItemsViewController

-(instancetype)init
{
//Call the superclass’s designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (int i = 0; i < 5; i++) {
[[JDMItemStore sharedStore] createItem];
}
}

return self;

}

-(instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}

#pragma mark - tableview datasource and delegate methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
self.headers = @[@“More than 50”,@“Others”];

return [self.headers objectAtIndex:section];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return [[[JDMItemStore sharedStore] allItems] count];
if (section == 0) {
return 40;
}else {
return 40;
}
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“UITableViewCell”];

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

NSArray *items = [[JDMItemStore sharedStore] allItems];

NSMutableArray *moreThanFifty = [NSMutableArray array];

NSMutableArray *lessThanFifty = [NSMutableArray array];

for (int i = 0; i < [items count]; i++) {
    JDMItem *item = [items objectAtIndex:i];
    if (item.valueInDollars >= 50) {
        [moreThanFifty addObject:item];
    }else {
        [lessThanFifty addObject:item];
    }
}

if (indexPath.section == 0) {
    JDMItem *item = [moreThanFifty objectAtIndex:indexPath.row];
    cell.textLabel.text = [item description];

}
else if (indexPath.section == 1) {
JDMItem *item = [lessThanFifty objectAtIndex:indexPath.row];
cell.textLabel.text = [item description];
}

return cell;

}

-(void)viewDidLoad
{
[super viewDidLoad];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

}

@end
[/code]

Hope you can help me!


#2

False alarm, i already did it! My god that was tricky!