My Bronze Solution


#1

Heres another solution. Critiques welcome!

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

@class BNRItem;

@interface BNRItemStore : NSObject

@property (nonatomic, readonly) NSArray *allItems;
@property (nonatomic, readonly) NSArray *cheapItems;
@property (nonatomic, readonly) NSArray *expensiveItems;

+(instancetype)sharedStore;
-(BNRItem *)createItem;

@end[/code]

BNRItemStore.m

[code]#import “BNRItemStore.h”
#import “BNRItem.h”

@interface BNRItemStore ()

@property (nonatomic) NSMutableArray *privateItems;
@property (nonatomic) NSMutableArray *privateCheapItems;
@property (nonatomic) NSMutableArray *privateExpensiveItems;

@end

@implementation BNRItemStore

+(instancetype)sharedStore
{
static BNRItemStore *sharedStore = nil;

if (!sharedStore) {
    sharedStore = [[self alloc] initPrivate];
}

return sharedStore;

}

// If a programmer calls [[BNRItemStore alloc] init] let them know the error of their ways
-(instancetype)init
{
@throw [NSException exceptionWithName:@“Singleton” reason:@“Use +[BNRItemStore sharedStore]” userInfo:nil];
return nil;
}

// Here is the real secret intializer
-(instancetype)initPrivate
{
self = [super init];
if (self) {
_privateItems = [[NSMutableArray alloc] init];
_privateCheapItems = [[NSMutableArray alloc] init];
_privateExpensiveItems = [[NSMutableArray alloc] init];
}

return self;

}

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

-(NSArray *)expensiveItems
{
return self.privateExpensiveItems;
}

-(NSArray *)cheapItems
{
return self.privateCheapItems;
}

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

if (item.valueInDollars < 50) {
    [self.privateCheapItems addObject:item];
    
}else{
    [self.privateExpensiveItems addObject:item];
}
return item;

}

@end[/code]

And lastly, BNRItemsViewController.m

[code]#import “BNRItemsViewController.h”
#import “BNRItem.h”
#import “BNRItemStore.h”

@implementation BNRItemsViewController

-(instancetype)init{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
for (int i = 0; i < 15; i++) {
[[BNRItemStore sharedStore]createItem];
}
}
NSLog(@“All Items: %@”,[[BNRItemStore sharedStore]allItems]);
NSLog(@“Expensive Items: %@”,[[BNRItemStore sharedStore]expensiveItems]);
NSLog(@“Cheap Items: %@”,[[BNRItemStore sharedStore]cheapItems]);
return self;
}

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

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

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell” forIndexPath:indexPath];

if (indexPath.section == 0) {
    NSArray *cheapItems = [[BNRItemStore sharedStore]cheapItems];
    BNRItem *cItem = cheapItems[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"$%d - %@",cItem.valueInDollars,cItem.description];
} else {
    NSArray *expensiveItems = [[BNRItemStore sharedStore] expensiveItems];
    BNRItem *eItem = expensiveItems[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"$%d - %@",eItem.valueInDollars,eItem.description];
}

return cell;

}

-(void)viewDidLoad
{
[super viewDidLoad];

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

}

@end[/code]