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

  • SEARCH
  • Home
  • 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 854037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:51:25+00:00 2026-05-15T07:51:25+00:00

I’ve searched and read and still haven’t found a concrete answer. Brief: I have

  • 0

I’ve searched and read and still haven’t found a concrete answer.

Brief:
I have an application where I declare an NSMutableArray in my AppDelegate to synthesize when the application loads. (code below). I have a SecondaryViewController call this function, and I have it output a string to let me know what the array size is. Every time I run it, it executes but it does not add any objects to the array. How do I fix this?

AppDelegate.h file

#import <UIKit/UIKit.h>

@class arrayTestViewController;

@interface arrayTestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    arrayTestViewController *viewController;

    NSMutableArray *myArray3;
}

@property (nonatomic, retain) NSMutableArray *myArray3;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet arrayTestViewController *viewController;

-(void)addToArray3;

@end

AppDelegate.m file

#import "arrayTestAppDelegate.h"
#import "arrayTestViewController.h"

@implementation arrayTestAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize myArray3;

#pragma mark -
#pragma mark Application lifecycle



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    myArray3 = [[NSMutableArray alloc] init];


    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

-(void)addToArray3{

    NSLog(@"Array Count: %d", [myArray3 count]);
    [myArray3 addObject:@"Test"];
    NSLog(@"Array triggered from SecondViewController");
    NSLog(@"Array Count: %d", [myArray3 count]);


}

SecondViewController.m file

#import "SecondViewController.h"
#import "arrayTestAppDelegate.h"

@implementation SecondViewController



-(IBAction)addToArray{
    arrayTestAppDelegate *object = [[arrayTestAppDelegate alloc] init];
    [object addToArray3];
}

EDIT***

Ok so This is what I tried:

arrayTestViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface arrayTestViewController : UIViewController {
    NSMutableArray *myArray;
    @private NSMutableArray *myArray2;
}

-(IBAction)showArray;
-(IBAction)switchViews;
-(IBAction)addToArray;


@end

arrayTestViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    myArray2 = [[NSMutableArray alloc] init];
    SecondViewController.myArray2 = myArray2;
    //ERRORS:Accessing unknown 'setMyArray2:" class method.
    //object cannot be set - either readonly property or no setter found
}

-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    screen.myArray2 = self.myArray2;
    //ERROR:Accessing unknown 'myArray2' getter method.

    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

SecondViewController.h

#import <UIKit/UIKit.h>
#import "arrayTestViewController.h"


@interface SecondViewController : UIViewController{
    NSMutableArray *myArray2;
}

@property (nonatomic, retain) NSMutableArray *myArray2;

-(IBAction)addToArray;
-(IBAction)switchBack;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "arrayTestViewController.h"

@implementation SecondViewController
@synthesize myArray2;


-(IBAction)addToArray{  
    arrayTestViewController *object = [[arrayTestViewController alloc] init];
    [object addToArray2];

}

It’s giving me three errors (also commented in the code):

Accessing unknown ‘setMyArray2:” class method. object cannot be set – either readonly property or no setter found. Accessing unknown ‘myArray2’ getter method.


I initially had tried this in the SecondViewController as well but the same outcome.

Thank you for your suggestion on not keeping them in the delegate 🙂

How would I fix this, given that I change it so that I initialize the Array in the the RootViewController, and I call the function from the SecondViewController?

  • 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-15T07:51:26+00:00Added an answer on May 15, 2026 at 7:51 am
    -(IBAction)addToArray{
        arrayTestAppDelegate *object = [[arrayTestAppDelegate alloc] init];
        [object addToArray3];
    }
    

    The above code is wrong. You should not be alloc‘ing an app delegate inside a view controller. Like, never. Ever. (Also, you leak memory each time, since you didn’t pair your alloc/init with a release. But I digress…)

    If you want to access your application delegate from your view controllers, you do it like so:

    arrayTestAppDelegate* delegate = (arrayTestAppDelegate*)[[UIApplication sharedApplication] delegate];
     [delegate addToArray];
    

    While that answers your question, I strongly urge you to not use your application delegate as a holder for global variables. While it’s not “bad” to create top-level arrays and such in your app delegate, you should be passing those objects down the view controller hierarchy as you create each view controller. This is cleaner in the sense that you do not have actions taking place at a distance. (Down with Singletons!)

    (P.S. In Cocoa, the convention is that class names begin with uppercase letters — arrayTestAppDelegate should be renamed ArrayTestAppDelegate)

    Edit:

    How would I fix this, given that I change it so that I initialize the Array in the the RootViewController, and I call the function from the SecondViewController?

    First, the object that owns an object should probably be the one responsible for initializing the object. Don’t put your array in your app delegate and then initialize it elsewhere. This is another form of “action at a distance” and is hard to read and debug later.

    Second, to answer your question really requires more information about how your “RootViewController” and “SecondViewController” exist in your controller hierarchy. But I can give you a general answer about passing objects (like your array) along the line.

    (I’m assuming that your RootViewController is the top-level view controller in your app, and that SecondViewController is a view controller shown by RootViewController. Judging by the names, it’s a safe bet.)

    ArrayTestAppDelegate.h (note the capitalization) might look something like this:

    ...
    @interface ArrayTestAppDelegate : NSObject <UIApplicationDelegate>
    {
        ...
    @private
        NSMutableArray* theArray;
        ...
    }
    ...
    

    Note the lack of @property.

    ArrayTestAppDelegate.m might look something like this:

    ...
    @implementation ArrayTestAppDelegate
    ...
    - (void)applicationDidFinishLaunching:(UIApplication*)application
    {
        ...
        theArray = [[NSMutableArray alloc] init];
        ...
        rootViewController.theArray = theArray;
    
        [window addSubview:rootViewController.view];
        [window makeKeyAndVisible];
    }
    ...
    - (void)dealloc
    {
        [theArray release];
    }
    ...
    @end
    

    RootViewController.h:

    ...
    @interface RootViewController : UITabBarController
    {
        ...
        NSMutableArray* theArray;
        ...
    }
    ...
    @property (nonatomic, retain) NSMutableArray* theArray;
    ...
    

    RootViewController.m

    @implementation RootViewController
    ...
    @synthesize theArray;
    ...
    - (void)dealloc
    {
        [theArray release];
    }
    ...
    - (void)showSecondViewController
    {
        SecondViewController* secondViewController = [[SecondViewController alloc] initWithNib:@"SecondView"];
        secondViewController.theArray = self.theArray;
        // Show secondViewController here -- modally, using nav controller stack, etc.
        [secondViewController release];
    }
    ...
    @end
    

    And so on. I leave to the reader the exercise of implementing theArray @property for SecondViewController (hint: much like RootViewController)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.