Help in Bronze challenge


#1

Hi i still couldn’t get it working. I have no idea why when i run the program it shows all my items as "( " and thats it no more. Very puzzled whats wrong with my code here. Hope you guys can help me out.

Thanks.

BNRItemStore.m

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

//create class extension coz only this class access this variable no other class need to know about it
@interface BNRItemStore ()

@property(nonatomic) NSMutableArray *privateItems;
@property(nonatomic)NSMutableArray *itemAbove50;
@property(nonatomic)NSMutableArray *itemUnder50;

@end

@implementation BNRItemStore

+(instancetype)sharedStore
{
static BNRItemStore *sharedStore;

//do i need to create a sharedstore?
if(!sharedStore) {
    sharedStore = [[self alloc] initPrivate];
}
return sharedStore;

}

//if programmer calls [[BNRItemStore alloc] init], let him know the error
-(instancetype)init
{
[NSException raise:@"Singleton"
format:@“use + [BNRItemStore sharedStore]”];
return nil;
}

//the secret initialiser
-(instancetype)initPrivate
{
self = [super init];

if(self) {
    //instantiate property privateItems
    _privateItems = [[NSMutableArray alloc] init];
    _itemAbove50 = [[NSMutableArray alloc] init];
    _itemUnder50 = [[NSMutableArray alloc] init];
    
}
return self;

}

//overide getter method of allItems property
-(NSArray *)allItems
{
return [self.privateItems copy];
}

-(BNRItem *)createItem
{
BNRItem *item = [BNRItem randomItem];
//[self.privateItems addObject:item];
if(item.valueInDollars > 50) {
[self.itemAbove50 addObject:item];
}
else {
[self.itemUnder50 addObject:item];
}
[self.privateItems addObject:self.itemAbove50];
[self.privateItems addObject:self.itemUnder50];
return item;
}

-(NSString *)displayAbove50
{
NSString *aboveString = [[NSString alloc] initWithFormat:@"%@",
self.itemAbove50];
return aboveString;
}

@end
[/code]

BNRItemsViewController.m

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

@implementation BNRItemsViewController

//create designated initialiser
-(instancetype)init
{
//always conform to 2 rules below
//call superclass designated initaliser
self = [super initWithStyle:UITableViewStylePlain];

if(self) {
    for(int i=0; i<5; i++) {
        [[BNRItemStore sharedStore] createItem];
    }
}
return self;

}

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

//method overriden
-(void)viewDidLoad
{
[super viewDidLoad];

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

NSLog(@"%@",[[BNRItemStore sharedStore] displayAbove50]);

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    NSLog(@“total items for allitems array are %d”,[[[BNRItemStore sharedStore] allItems] count]);
    return [[[BNRItemStore sharedStore] allItems] count];
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //get a new or recycle cell
    //UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
    // reuseIdentifier:@“UITableViewCell”];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“UITableViewCell” forIndexPath:indexPath];

    //set the text of the cell with the description of the item
    NSArray *items = [[BNRItemStore sharedStore] allItems];
    BNRItem *item = items[indexPath.row];

    cell.textLabel.text = [item description];

    return cell;
    }

@end
[/code]


#2

While I wouldn’t have expected only a single paren to be the result, I can say this much:

“itemAbove50” is actually an ARRAY that the code is trying to stuff into a single string. If you really want that to happen, I think you’ll have to do it yourself.

In any case, the big thing is that you’ll want to iterate over each item in the array. Whether you stuff them into a single string or change the design to NSLog() each item individually is up to you.