Keep getting EXC_BAD_ACCESS Bronze challenge


#1

I keep getting this error no matter what pls help.
in BNRItemsViewController.m

#import "BNRItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"

@implementation BNRItemsViewController

-(instancetype)init
{
    
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        for (int i = 0; i < 5; i++) {
            [[BNRItemStore sharedStore]createItem];
        }
    }
    return self;

}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        int rows = [[BNRItemStore sharedStore].lessThan50 count];
        return rows;
    }else{
        int rows = [[BNRItemStore sharedStore].moreThan50 count];
        return rows;
    }
    
}

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


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get a new or recycles cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
  
    //Set the text on the cell with the description of the item
    //that is at the nth index of items, where n = row this cell
    //will appear in on the tableview
    if (indexPath.section == 0) {
        NSMutableArray *items = [BNRItemStore sharedStore].lessThan50;
        BNRItem *h = items[indexPath.row];
        cell.textLabel.text = [h description];
    }else{
        NSMutableArray *items = [BNRItemStore sharedStore].moreThan50;
        BNRItem *h = items[indexPath.row];
        cell.textLabel.text = [h description];
    }
    return cell;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}


@end

BNRItemStore.h

[code]#import <Foundation/Foundation.h>

@class BNRItem;
@interface BNRItemStore : NSObject

@property (nonatomic, readonly) NSArray *allItems;
@property (nonatomic) NSMutableArray *lessThan50;
@property (nonatomic) NSMutableArray *moreThan50;

//Notice that this is a class method and prefixed with a + instead
// of a -
+(instancetype)sharedStore;
-(BNRItem *)createItem;
-(NSMutableArray *)lessThan50;
-(NSMutableArray *)moreThan50;

@end
[/code]

BNRItemStore.m

#import "BNRItemStore.h"
#import "BNRItem.h"

@interface BNRItemStore()

@property (nonatomic) NSMutableArray *privateItems;


@end


@implementation BNRItemStore

+(instancetype)sharedStore
{
    static BNRItemStore *sharedStore = nil;
    //Do I need to create a sharedStore?
    if (!sharedStore) {
        sharedStore = [[self alloc]initPrivate];
    }
    return sharedStore;
}

//If a programmer calls [[BNRItemStore alloc]init], let him
//know the error of his ways
-(instancetype)init
{
    @throw [NSException exceptionWithName:@"Singleton" reason:@"Use+[BNRItemStore sharedStore]" userInfo:nil];
}

//Here is the real (secret) initializer
-(instancetype)initPrivate
{
    self = [super init];
    if (self) {
        _privateItems = [[NSMutableArray alloc]init];
        _moreThan50 = [[NSMutableArray alloc]init];
        _lessThan50 = [[NSMutableArray alloc]init];
    }
    return self;
}

-(NSArray *)allItems
{
    return self.privateItems;
}

-(BNRItem *)createItem
{
    BNRItem *item = [BNRItem randomItem];
    [self.privateItems addObject:item];
    return item;
}

-(NSMutableArray *)lessThan50
{
        for (BNRItem *h in self.privateItems) {
        if (h.valueInDollars < 50) {
            [self.lessThan50 addObject:h];
        }
    }
    return self.lessThan50;
}

-(NSMutableArray *)moreThan50
{
    
    for (BNRItem *h in self.privateItems) { //I'm getting error here all the time.. EXC_BAD_ACCESS
        if (h.valueInDollars > 50) {
            [self.moreThan50 addObject:h];
        }
    }
    return self.moreThan50;
}

@end

#2

I think the issue is you are adding items to the moreThan50 / lessThan50 arrays every time the getter is getting called. So the arrays keep increasing in size and so there will eventually be a discrepancy somewhere.

What might help solve the problem (although it might not be optimal, but should work with your current solution) is to remove all of the previous items from the array before adding the new items. So you’d do [_moreThan50 removeAllObjects]; as the first line of your - (NSMutableArray)moreThan50 { } method (and same for less than).


#3

Thanks! actually i already solved this thing after posting this problem.