Adding an BNRItem to itemContainer


#1

Hi,

I habe the following setup. What I do not understand ist the following:

In the main.m I succesfully create 10 random BNRItems and I pass each one do the addItemToContainer method of BNRContainer class. Why doesnt it work that the property itemContainer accepts the items and stores the pointers to them?

//main.m
#import "BNRItem.h"
#import "BNRContainer.h"

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

    @autoreleasepool {
        BNRContainer *derContainer = [[BNRContainer alloc]init];
     
        // Create 10 random items
        for (int i = 0; i<10; i++){
            BNRItem *r = [BNRItem randomItem];
       
            
            

            
            [derContainer addItemToContainer:r];
            
            }
    
        NSLog (@"%@", [derContainer itemContainer]);
   
    }
    return 0;
}
//BNRContainer.h
#import "BNRItem.h"

@interface BNRContainer : BNRItem

@property (nonatomic, strong)               NSMutableArray *itemContainer;

-(void)addItemToContainer:(BNRItem *)item;



@end[/code]

[code]//BNRContainer.m
#import "BNRContainer.h"

@implementation BNRContainer 
@synthesize itemContainer;


-(void)addItemToContainer:(BNRItem *)item{
    [itemContainer addObject:item];
    // NSLog(@"%@", item);
  //  NSLog(@"%@", itemContainer);

}
@end

The result ist: 2012-08-03 17:05:24.077 RandomPosessions[9679:303] (null)

Please help.


#2

Is it because I did not create an instance for itemContainer in the init method? e.g. like this:

-(id)init{ itemContainer = [[NSMutableArray alloc] init]; return self; }


#3

That’s it! However, your init method is unorthodox; it should invoke the super’s init or some other init method that’s more specialised:

-(id)init
{
    self = [super init];
    if (self)
    {
        itemContainer = [[NSMutableArray alloc] init];
    }
    return self;
}

#4

Thank you for the additional help. I will stick to orthodox ways now =)