Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7309019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:39:20+00:00 2026-05-28T23:39:20+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:39:21+00:00Added an answer on May 28, 2026 at 11:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After many hours of hair pulling, I look to stackoverflow to help me solve
Still trying to solve this after many hours. The script works in jfiddle: http://jsfiddle.net/uCcYw/3/
Trying to parse an nested expressions like GroupParser.parse({{a}{{c}{d}}}) After many hours i have now
I'm totally confused after trying to solve my problem for several hours for myself,
I'm trying to solve an AVAudioSession problem since many hours ago and didn't get
After many hours of messing about and trying to set up what should be
After spending many hours poking around trying to get an ASP .NET AJAX proxy
So after wasting many hours trying to get my app to run in landscape
After googling for many hours for a solution for the above Sharepoint exception, I
I'm trying to start unicorn_rails in a ruby script, and after executing many commands

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.