iPhone:在 AppDelegate 中使用 NSMutableArry 作为全局变量
我想要完成的是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事:
首先,在应用程序委托中,您需要确保在任何对象尝试访问数组之前初始化该数组。客户接待员对此很有帮助。
现在,对该属性的任何调用都保证返回一个数组。
在视图控制器中,您需要定义属性来保存数组。由于该数组由应用程序委托保存并且始终存在,因此最好分配该数组而不是保留它。这样,您始终知道您正在写入完全相同的数组,并且应用程序委托可以完全控制其生命周期。
在视图控制器中:
然后每次引用它时,请确保使用自我表示法:
话虽如此,粘贴数组或任何其他内容是非常糟糕的做法愚蠢的数据对象会在你的应用程序中赤裸裸地付出代价。您无法控制每段代码将对数组执行什么操作。您必须在向数组添加或删除数据的每个位置复制所有验证代码。
最好将数组包装在自定义类中并使其受保护,这样它只能由类方法更改。
像这样:
scrap.m
然后将自定义类添加到应用程序委托的 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.
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:
then every time you reference it make sure to use the self notation:
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:
scratch.m
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.
这里有几个问题:
self 声明以保留应用程序委托数组
相反,请尝试:
There are a couple problems here:
self
declaration to retain the app delegate arrayInstead, try: