3

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
1
  • try to write alloc and init of array in viewDidLoad Commented Feb 3, 2012 at 10:27

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Also, in viewDidUnload, self.fruitArr won't work. You didn't declare it as a property.
it's initWithNibName:bundle: for UIViewControllers, not initWithCoder:.
If the view controller was initialized through Interface Builder, it will use initWithCoder. If it's I initialized manually, then it's up to the person who creates it to call whatever function. The person isn't necessarily initializing it with a nib.
sorry, I thought only the views have NSCoding protocol, not the viewcontrollers.
No problem, just thought I'd clarify instead of being a giant butt. My last answer was a bit rude, sorry about that.
|
2

Use view did load instead of init...

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

Comments

0

Change that init for viewDidLoad and see what happens

Comments

0

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).

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.