Andy
Andy

Reputation: 727

NSMutableArray as instance variable alway null

After many hours wasted, I officially turn to the experts for help!

My problem lies with using a NSMutableArray as an instance variable, and trying to both add objects and return the array in a method in my class. I am obviously doing something fundamentally wrong and would be grateful for help...I have already tried all the suggestions from other similar questions on stackoverflow, read apples documentation, and basically all combinations of trial and error coding I can think of. The mutable array just alway returns (null). I've even tried creating properties for them, but still the array returns (null) and then I also am running into memory management problems due to the retain while setting the property, and the init in the init method for the class.

Here is what I am trying to do:

1) Loop through a series of UISwitches and if they are 'switched on', add a string to the NSMutableArray

2) Assign this mutable array to another array in another method

Any help much appreciated,

Andy

And for some code...

fruitsViewController.h

#import <UIKit/UIKit.h>

@interface fruitsViewController : UIViewController
{
    NSMutableArray *fruitsArr;
    UISwitch *appleSwitch;
    UISwitch *orangeSwitch;
}

@property (nonatomic,retain) NSMutableArray *fruitsArr; // ADDED ON EDIT
@property (nonatomic,retain) IBOutlet UISwitch *appleSwitch;
@property (nonatomic,retain) IBOutlet UISwitch *orangeSwitch;

- (IBAction)submitButtonPressed:(id)sender;
@end

fruitsViewController.m

#import "fruitsViewController.h"

@implementation fruitsViewController

@synthesize fruitsArr; // ADDED ON EDIT
@synthesize appleSwitch, orangeSwitch;

/* COMMENTED OUT ON EDIT
-(id)init
{
    if (self = [super init]) {
        // Allocate memory and initialize the fruits mutable array
        fruitsArr = [[NSMutableArray alloc] init];
    }
    return self;
}
*/

// VIEW DID LOAD ADDED ON EDIT
- (void)viewDidLoad
{
    self.fruitsArr = [[NSMutableArray alloc] init];
}

- (void)viewDidUnload
{
    self.fruitsArr = nil;
    self.appleSwitch = nil;
    self.orangeSwitch = nil;
}

- (void)dealloc
{
    [fruitsArr release];
    [appleSwitch release];
    [orangeSwitch release];
    [super dealloc];
}

- (IBAction)submitButtonPressed:(id)sender
{
    if ([self.appleSwitch isOn]) {
        [self.fruitsArr addObject:@"Apple"; // 'self.' ADDED ON EDIT
    }
    if ([self.orangeSwitch isOn]) {
        [self.fruitsArr addObject:@"Orange"; // 'self.' ADDED ON EDIT
    }
    NSLog(@"%@",self.fruitsArr); // Why is this returning (null) even if the switches are on?!
    [fruitsArr addObject:@"Hello World";
    NSLog(@"%@",self.fruitsArr); // Even trying to add an object outside the if statement returns (null)
}
@end

Upvotes: 3

Views: 1843

Answers (5)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Use view did load instead of init...

- (void)viewDidLoad
{
    fruitsArr = [[NSMutableArray alloc] init];
}

Upvotes: 2

Seamus
Seamus

Reputation: 243

because fruitsArr don't be init. you should do this first: fruitsArr = [[NSMutableArray alloc] init];

so, I think you don't run - (id)init before you use fruitsArr.

Upvotes: 0

alex-i
alex-i

Reputation: 5454

Is your init method ever being called (in complicationsViewController). Add a NSLog to check this, you might be calling initWithNib: maybe.

At viewDidUnload you should remove self.fruitsArr = nil;, or, if you want to keep it, then initialize the fruitsArr in viewDidLoad (and remove it from init).

Upvotes: 0

Simon Germain
Simon Germain

Reputation: 6844

It seems like your init function is never called. If you're initializing this view controller from a NIB, you need to use initWithCoder. If not, just declare your fruitsArr in viewDidLoad.

Upvotes: 7

whtlnv
whtlnv

Reputation: 2207

Change that init for viewDidLoad and see what happens

Upvotes: 0

Related Questions