Adding an BNRItem to itemContainer



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?

#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;
#import "BNRItem.h"

@interface BNRContainer : BNRItem

@property (nonatomic, strong)               NSMutableArray *itemContainer;

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


#import "BNRContainer.h"

@implementation BNRContainer 
@synthesize itemContainer;

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


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

Please help.


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; }


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:

    self = [super init];
    if (self)
        itemContainer = [[NSMutableArray alloc] init];
    return self;


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