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 732289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:07:28+00:00 2026-05-14T07:07:28+00:00

What i’m trying to accomplish is to have an NSMutableArray defined in the AppDelegate.

  • 0

What i’m trying to accomplish is to have an NSMutableArray defined in the AppDelegate. I then have two UIViewControllers. One view is responsible for displaying the array from the AppDelegate. The other view is used to add items to the array. So the array starts out to be empty. View1 doesn’t display anything because the array is empty. The User goes to View2 and adds an item to the array in AppDelegate. Then when the user goes back to View1 it now displays one item.

Here is how I’m trying to accomplish this

@interface CalcAppDelegate : NSObject <UIApplicationDelegate> {
 UIWindow *window;
 UITabBarController *tabBarController;
 NSMutableArray *globalClasses;
}
@property (nonatomic,retain) NSMutableArray *globalClasses;

My other view

In the viewDidload I set the array in my View to be the one in the AppDelegate. In an effort to retain values.

allCourses = [[NSMutableArray alloc]init];
CalcAppDelegate *appDelegate = (CalcAppDelegate *)[[UIApplication sharedApplication] delegate];
allCourses = appDelegate.globalClasses;

Then I would update my allCourses array by adding a new item. Then try to set the array in the AppDelegate to be equal to the modified one.

CalcAppDelegate *appDel = (CalcAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"Size before reset %d",[appDel.globalClasses count]);
    appDel.globalClasses = allCourses;
    NSLog(@"Size after reset %d",[appDel.globalClasses count]);

What I’m seeing that’s returned is 2 in the before, and 2 after. So it doesn’t appear to be getting updated properly. Any suggestions?

  • 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-14T07:07:28+00:00Added an answer on May 14, 2026 at 7:07 am

    A few things:

    First, in your app delegate you need to make sure that you intialize the array before any object tries to access it. A customer getter is good for this.

    -(void) getGlobalClasses{
        if (globalClasses!=nil) {
            return globalClasses;
        }
        NSMutableArray *newArray=[[NSMutableArray alloc] initWithCapacity:1]; //yes, I'm old school
        self.globalClasses=newArray;
        [newArray release];
        return globalClasses;
    }
    

    Now any call to the property is guaranteed to return an array.

    In your view controllers, you need to define properties to hold the array. Since the array is held by the app delegate and will always be there, it is best to assign the array instead of retaining it. That way you always know you are writing to the exact same array and the app delegate has complete control over its life cycle.

    In the view controllers:

    @property(nonatomic,assign) NSMutableArray *globalClasses;
    

    then every time you reference it make sure to use the self notation:

    self.globalClasses=//...whatever
    

    Having said all this, it is extremely bad practice to stick an array or any other dumb data object out their buck naked in your app. You have no control over what each piece of code will do to the array. You will have to duplicate all your validation code every place you add or remove data to the array.

    It would be better to wrap the array in a custom class and make it protected so it can only be altered by the classes methods.

    Like so:

    @interface MyData : NSObject {
    @protected
        NSMutableArray *myDataArray;
    }
    
    -(void) addObject:(id) anObject;
    -(void) removeObjectAtIndex;(NSInteger) anIndex;
    
    @end
    

    scratch.m

    @interface scratch ()
    @property(nonatomic, retain)  NSMutableArray *myDataArray;
    
    @end
    
    @implementation scratch
    @synthesize myDataArray;
    
    -(void) addObject:(id) anObject{
        //...code to check if anObject is a valid one to add to the array
        [self.myDataArray addObject:anObject];
    }//------------------------------------addObject:------------------------------------
    
    -(void) removeObjectAtIndex;(NSInteger) anIndex{
        //... do bounds checking and other testing to ensure no problems
        // will result from removing the object at the given idex
        [self.myDataArray removeObjectAtIndex:anIndex];
    }//-------------------------------------(void) removeObjectAtIndex;(NSInteger) anIndex------------------------------------
    

    Then add the custom class an a property of the app delegate as shown above. This will keep your data clean and modular so you can safely use it in a wide range of app reobjects without having to micromanage the array in every object.

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

Sidebar

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.