Stuck in gold challenge


#1

I am not sure if I am on the right track. Please have a look?

I have 2 arrays : items and containers.
ContainerItems have a property NSMutableArray *pack.
BNRContainer has a method AddItem: to Pack.
So I tried to add itemX to containerY, and log the results, but it looks like I didn’t pack ANYTHing into the container’s pack.

I think that is because I do not alloc-init the pack-array. I tried to alloc init it on several places, but it doesn’t work (mostly I get a warning that pack is not used, while I THINK I use it in different places already).
I am afraid I am missing something simple, but what?

BNRItem not changed.
BNRContainer.h

[code]#import “BNRItem.h”

@interface BNRContainer : BNRItem
@property (strong, nonatomic) NSMutableArray *pack;

  • (unsigned int)totalValue;
  • (unsigned int)valueContainer;
  • (void) addItem:(id)item;
    @end
    [/code]

BNRContainer.m

[code]@implementation BNRContainer
@synthesize pack;

  • (void) addItem:(id)item {
    [pack addObject:item];
    }

@end[/code]

Main.m


 #import <Foundation/Foundation.h>
#import "BNRItem.h"
#import "BNRContainer.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
                
        NSMutableArray *items = [[NSMutableArray alloc] init];
        NSMutableArray *containers = [[NSMutableArray alloc] init];
        NSMutableArray *pack = [[NSMutableArray alloc] init];  >>>>>>>>>WARNING: unused var pack <<<<<<<<<<<<<
        
        //create items
        BNRItem *Phone = [[BNRItem alloc]initWithItemName:@"Phone"
                                               valueInDollars:450
                                                 serialNumber:@"UT1"];
        BNRItem *iPad = [[BNRItem alloc]initWithItemName:@"iPad"
                                          valueInDollars:600
                                            serialNumber:@"Gadget1"];
        BNRItem *Suit = [[BNRItem alloc]initWithItemName:@"Business Suit"
                                          valueInDollars:1500
                                            serialNumber:@"CLTH1"];
        BNRItem *Chocolate =[[BNRItem alloc]initWithItemName:@"Chocolate Bar"
                                             valueInDollars:3
                                               serialNumber:@"FD1"];
        [items addObject:Phone];
        [items addObject:iPad];
        [items addObject:Suit];
        [items addObject:Chocolate];

       //create container items
        BNRContainer *samsonite = [[BNRContainer alloc]initWithItemName:@"Samsonite" valueInDollars:250 serialNumber:@"S1"];
       
        BNRContainer *birkin = [[BNRContainer alloc]initWithItemName:@"Birkin" valueInDollars:4500 serialNumber:@"B3"];
       
        BNRContainer *flightcase = [[BNRContainer alloc]initWithItemName:@"flightcase"
                                                     valueInDollars:99
                                                       serialNumber:@"F1"];
        BNRContainer *plasticBag = [[BNRContainer alloc]initWithItemName:@"plastic bag"
                                                     valueInDollars:1
                                                    serialNumber:@"PB25"];
        BNRContainer *closet = [[BNRContainer alloc]initWithItemName:@"cellar closet"
                                                      valueInDollars:300
                                                        serialNumber:@"CC1"];
        
        //add to array containers
        [containers addObject:samsonite];
        [containers addObject:birkin];
        [containers addObject:flightcase];
        [containers addObject:plasticBag];
        [containers addObject:closet];
        
        
        //put items in container
        [samsonite addItem:iPad];
        [birkin addItem:Phone];
        [flightcase addItem:Suit];
        [plasticBag addItem:Chocolate];
        
        //put plasticBag in birkin
        [birkin addItem:plasticBag];
        
        //store everything in the closet
        [closet addItem:samsonite];
        [closet addItem:birkin];
        [closet addItem:flightcase];
        
        
        NSLog(@"%@ contains %@: ", [samsonite itemName], [samsonite pack]);
        
        items = nil;
    }
    
    return 0;

}

Output:
013-08-14 19:01:22.302 BNR 1 RandomPossessions[42381:303] Samsonite contains (null):

Is it the init:method voor Pack the problem, is it the wrong place for this init: or is it the addItem: method?


#2

You are creating a pack array in main () but not assigning it to a pack property.

I think you meant to do this:

BNRContainer *samsonite = [[BNRContainer alloc]initWithItemName:...];
samsonite.pack = [NSMutableArray array];

BNRContainer *birkin = [[BNRContainer alloc]initWithItemName:...];
birkin.pack = [NSMutableArray array];
...

#3

Yes, I think so too :wink:
Thanks, I see my mistake now: because I declared pack as an instance var, I have to access it by a setter method.
And I was not doing that.

Is this an example of the concept: ‘an instance variable that is an object’ ?

Thnks