iPhone:在 AppDelegate 中使用 NSMutableArry 作为全局变量

发布于 2024-08-28 19:31:40 字数 1176 浏览 2 评论 0原文

我想要完成的是在 AppDelegate 中定义 NSMutableArray。然后我有两个 UIViewController。一个视图负责显示来自 AppDelegate 的数组。另一个视图用于将项目添加到数组中。所以数组开始是空的。 View1 不显示任何内容,因为数组为空。用户转到 View2 并向 AppDelegate 中的数组添加一个项目。然后,当用户返回 View1 时,它现在显示一个项目。

这是我试图完成此任务的方法

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

我的另一个视图

在 viewDidload 中,我将视图中的数组设置为 AppDelegate 中的数组。努力保留价值观。

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

然后我将通过添加新项目来更新我的 allCourses 数组。然后尝试将 AppDelegate 中的数组设置为等于修改后的数组。

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]);

我看到返回的是之前的 2 个,之后的 2 个。所以它似乎没有得到正确更新。有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒与心事 2024-09-04 19:31:40

有几件事:

首先,在应用程序委托中,您需要确保在任何对象尝试访问数组之前初始化该数组。客户接待员对此很有帮助。

-(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;
}

现在,对该属性的任何调用都保证返回一个数组。

在视图控制器中,您需要定义属性来保存数组。由于该数组由应用程序委托保存并且始终存在,因此最好分配该数组而不是保留它。这样,您始终知道您正在写入完全相同的数组,并且应用程序委托可以完全控制其生命周期。

在视图控制器中:

@property(nonatomic,assign) NSMutableArray *globalClasses;

然后每次引用它时,请确保使用自我表示法:

self.globalClasses=//...whatever

话虽如此,粘贴数组或任何其他内容是非常糟糕的做法愚蠢的数据对象会在你的应用程序中赤裸裸地付出代价。您无法控制每段代码将对数组执行什么操作。您必须在向数组添加或删除数据的每个位置复制所有验证代码。

最好将数组包装在自定义类中并使其受保护,这样它只能由类方法更改。

像这样:

@interface MyData : NSObject {
@protected
    NSMutableArray *myDataArray;
}

-(void) addObject:(id) anObject;
-(void) removeObjectAtIndex;(NSInteger) anIndex;

@end

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

然后将自定义类添加到应用程序委托的 a 属性中,如上所示。这将使您的数据保持干净和模块化,以便您可以在各种应用程序重新对象中安全地使用它,而无需对每个对象中的数组进行微观管理。

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.

地狱即天堂 2024-09-04 19:31:40

这里有几个问题:

allCourses = [[NSMutableArray alloc] init];
CalcAppDelegate *appDelegate = (CalcAppDelegate *)[[UIApplication sharedApplication] delegate];
allCourses = appDelegate.globalClasses;
  1. 您不需要分配一个新数组,因为您想保留现有数组(应用程序委托中的数组)
  2. 如果您使用属性,则需要使用 self 声明以保留应用程序委托数组

相反,请尝试:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文