Having problems with adding objects to NSMutableArray


I am having problem with adding objects to NSMutableArray *array.

//  Controller.m
#import "Controller.h"
@implementation Controller
- (void)parser:(NSString *)string{
    [array addObject:string];
    NSLog(@"answerArray(1): %@",[array objectAtIndex:1]);
    [array retain];

//  Controller.h
#import <Foundation/Foundation.h>
@interface Controller : NSObject {
    NSMutableArray *array;
- (void)parser:(NSString *)string;

NSLog(@"answerArray(1): %@",[array objectAtIndex:1]);

Results: answerArray(1): (null)

First off, you're over-retaining the array.

Second, you didn't provide the code for initializing the array, so I guess it's not allocated and initialized. This will cause the code to message a nil object and thus return nil.

You should create an init method for the Controller object, and allocate a new NSMutableArray object (and retain it).

Also, a proper dealloc to release the array.