Q:

Adding a temporary object to an NSMutableArray

I have a custom class MDRect that i am trying to add an NSMutableArray
the array is a property: 
@property (retain) NSMutableArray* array; 

it is initialized in the initMethod of the NSView subclass:
-(id)init {
array =  [NSMutableArray new];
return [super init];
 }

then i am trying to add an object in the array here:
-(void)mouseUp:(NSEvent *)theEvent
{
NSPoint mouseLoc = [NSEvent mouseLocation];    
mouseLoc = [self mouse:mouseLoc inRect:[self frame]];

CGSize temp;
NSLog(@"%f",mouseLoc.y - mouseLocation.y);
NSLog(@"%f",mouseLoc.x - mouseLocation.x);
temp.height = mouseLoc.y - mouseLocation.y;
temp.width = mouseLoc.x - mouseLocation.x;

tempRect.size = temp;
MDRect * rect = [[MDRect alloc] initWithColor:[NSColor orangeColor] andRect:tempRect];
[array addObject:rect];    

int i =     (int)array.count;
NSLog(@"%i",i);
[self setNeedsDisplay:YES];
}

But the object being is not being added to the array. it never returns any value other than 0 in the NSLog function. What am I doing wrong?

A:

Your problem is your init method. You've got it quite wrong and it should look like this:
- (id)init {
    if (self = [super init]) {
        array = [NSMutableArray new];
    }
    return self;
}

Your problem is that you're not calling super's init and assigning that to self and then setting up array. You're assigning array before you've even got a proper object and then you're not even returning anything that array has been set on, but rather returning the result of the super-class's init method. Then when you go to log array.count, array is nil and hence i becomes 0 (because messaging nil returns 0 in this circumstance).

