iPhone: Using a NSMutableArry in the AppDelegate as a Global Variable

北慕城南 提交于 2020-01-01 07:24:09

问题


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:


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.




回答2:


There are a couple problems here:

allCourses = [[NSMutableArray alloc] init];
CalcAppDelegate *appDelegate = (CalcAppDelegate *)[[UIApplication sharedApplication] delegate];
allCourses = appDelegate.globalClasses;
  1. You do not need to allocate a new array since you want to retain an existing array (the one in the app delegate)
  2. If you're using properties, you need to use the self declaration to retain the app delegate array

Instead, try:

CalcAppDelegate *appDelegate = (CalcAppDelegate *)[[UIApplication sharedApplication] delegate];
self.allCourses = appDelegate.globalClasses;


来源:https://stackoverflow.com/questions/2655033/iphone-using-a-nsmutablearry-in-the-appdelegate-as-a-global-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!