Bronze Solution (works)

#1

This one was a little tricky at first. I urge you to try to figure it out on your own before looking at the following solution.

Here is my BNRItemStore.h:

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

@class BNRItem;

@interface BNRItemStore : NSObject
@property (nonatomic, readonly) NSArray *allItems;

  • (instancetype)sharedStore;
  • (BNRItem *)createItem;
  • (NSArray *)itemsOver50;
  • (NSArray *)itemsUnder50;

@end[/code]

Here is my BNRItemStore.m:

[code]#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 an item store?
    if (!sharedStore) {
    sharedStore = [[self alloc] initPrivate];
    }

    return sharedStore;
    }

// Throw an exception here, since BNRItemStore is a Singleton

  • (instancetype)init {
    @throw [NSException exceptionWithName:@“Singleton” reason:@“Use +[BNRItemStore sharedStore]” userInfo:nil];
    return nil;

}

  • (instancetype)initPrivate {
    self = [super init];

    if (self) {
    _privateItems = [[NSMutableArray alloc] init];
    }
    return self;
    }

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

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

    return item;

}

  • (NSArray *)itemsOver50 {
    NSMutableArray *over50 = [[NSMutableArray alloc] init];

    for (BNRItem *item in _privateItems) {
    if (item.valueInDollars > 50) {
    [over50 addObject:item];
    }
    }

    return over50;
    }

  • (NSArray *)itemsUnder50 {
    NSMutableArray *under50 = [[NSMutableArray alloc] init];

    for (BNRItem *item in _privateItems) {
    if (item.valueInDollars <= 50) {
    [under50 addObject:item];
    }
    }

    return under50;
    }
    @end
    [/code]

Here is my BNRItemsViewController.m:


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

@implementation BNRItemsViewController

- (instancetype)init {
    // Call superclass's designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    
    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) {
        return [[[BNRItemStore sharedStore] itemsOver50] count];
    } else if (section == 1) {
        return [[[BNRItemStore sharedStore] itemsUnder50] count];
    }
    return 0;
}

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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section
{
    if(section == 0) {
        return @"Items over $50";
    }
    else {
        return @"Items under $50";
        
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Create an instance of UITableViewCell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        NSArray *over50 = [[BNRItemStore sharedStore] itemsOver50];
        BNRItem *item = over50[indexPath.row];
        cell.textLabel.text = item.description;
    } else if (indexPath.section == 1) {
        NSArray *under50 = [[BNRItemStore sharedStore] itemsUnder50];
        BNRItem *item = under50[indexPath.row];
        cell.textLabel.text = item.description;
    }
    
    return cell;
}

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

@end

All of the other files are unchanged.

#2

Thanks for your solution,it help me a lot.But why do you say it’s a trick?