NSMutableDictionary 从不同的类访问
在我的 iPhone 应用程序中,我使用整数来跟踪大量变量。我在 AppDelegate 文件(它是一个多视图应用程序)中声明并初始化了它们,然后如果我在其他视图(类)中声明它们,那么这些值将保持不变。通过这种方式,我可以在 App Delegate 文件中设置 Money = 200,然后在另一个视图中声明“int Money;”并且它已经设置为 200(或者无论 Money 是什么)。
但是,如果我将所有这些变量存储在字典中(我现在正在这样做),我如何从不同的类/视图访问该字典?我不能简单地再次“声明”它,我已经尝试过了。我认为这与字典是一个对象有关,因此需要引用它或其他东西。
我需要能够从所有不同的视图访问相同的字典。
#import "SheepAppDelegate.h"
@implementation SheepAppDelegate
@synthesize window;
@synthesize rootController;
//Initialize the Dictionary to store all of our variables
NSMutableDictionary *theHeart;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//Set the values for the Variables and Constants, these are
//accessed from the different classes.
NSMutableDictionary *theHeart = [[NSMutableDictionary alloc] init];
[theHeart setObject:[NSNumber numberWithInt:200] forKey:@"Money"];
[theHeart setObject:@"Number two!" forKey:@"2"];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}
初始化字典并向其中添加内容工作正常,但在另一个类中。
#import "OverviewController.h"
@implementation OverviewController
@synthesize lblMoney;
@synthesize lblSheep;
@synthesize lblWool;
@synthesize lblFatness;
@synthesize lblCapacity;
@synthesize lblShepherds;
int varMoney;
NSMutableDictionary *theHeart;
- (void)viewWillAppear:(BOOL)animated {
varMoney = [[theHeart objectForKey:@"Money"] intValue];
}
您可以看到我尝试再次为此类初始化字典,但这显然不起作用。我只想在 AppDelegate 文件中初始化和设置字典一次,然后从其他类访问该字典以更改其中的内容。有办法做到这一点吗?
In my iPhone app I was using integers to keep track of a lot of variables. I declared and initialized them all in my AppDelegate file (it's a multiview app), and then if I declared them in the other views (classes) and the values would stay the same. In this way, I could set Money = 200 in the App Delegate file, and then in another view just declare, "int Money;" and it would be set to 200 already (or whatever Money happened to be.)
However, if I'm storing all these variables in a Dictionary (which I am doing now), how can I access that dictionary from different classes/views? I can't simple "declare" it again, I've already tried that. I think it has to do with the dictionary being an object, and so it needs to be referenced or something.
I need to be able to access the same dictionary from all the different views.
#import "SheepAppDelegate.h"
@implementation SheepAppDelegate
@synthesize window;
@synthesize rootController;
//Initialize the Dictionary to store all of our variables
NSMutableDictionary *theHeart;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//Set the values for the Variables and Constants, these are
//accessed from the different classes.
NSMutableDictionary *theHeart = [[NSMutableDictionary alloc] init];
[theHeart setObject:[NSNumber numberWithInt:200] forKey:@"Money"];
[theHeart setObject:@"Number two!" forKey:@"2"];
[window addSubview:rootController.view];
[window makeKeyAndVisible];
}
Initialize the dictionary and adding stuff to it works fine, but in another class.
#import "OverviewController.h"
@implementation OverviewController
@synthesize lblMoney;
@synthesize lblSheep;
@synthesize lblWool;
@synthesize lblFatness;
@synthesize lblCapacity;
@synthesize lblShepherds;
int varMoney;
NSMutableDictionary *theHeart;
- (void)viewWillAppear:(BOOL)animated {
varMoney = [[theHeart objectForKey:@"Money"] intValue];
}
You can see I try initializing the dictionary again for this class, but that obviously isn't working. I just want to Initialize and setup the dictionary once, in the AppDelegate file, and then access that dictionary from the other classes to change stuff in it. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 NSMutableDictionary 实例设为静态并编写一个类方法来访问它。将其放入您的 SheepAppDelegate.m 中:
并使用以下方法在其他任何地方访问它:
Make your NSMutableDictionary instance static and write a class method to access it. Put this in your SheepAppDelegate.m:
And access it anywhere else by using:
您可以将其放入 AppDelegate 中或创建一个 Singleton。 本文涵盖了这个主题以及许多可能的内容选项包括我提到的两个。
单例似乎是更有组织的方法。您可以将所有全球信息存储在一个信息中,并且可以从任何地方访问它。
You can either put it in your AppDelegate or create a Singleton. This article covers this topic and many of the possible options including the two I mentioned.
Singletons seem to be the more organized method. You can store all of your global information in one and you'll be able to access it from anywhere.
没有充分的理由不将字典传递给控制器作为参考。如果您在 OverviewController 中创建一个 NSMutableDictionary ivar 使其成为一个属性,那么您可以在创建控制器或从笔尖解冻时在那里设置字典。
单例很有用,但除非你真的需要它,否则我不会诉诸它。您可以将 -applicationDidFinishLaunching 更改为如下所示:
这假设您的 rootController 属于 OverviewController 类型。然后在您的 OverviewController 标头中,您应该声明一个如下所示的属性:
@property (assign) NSMutableDictionary *heartDictionary;
然后使用 @synthesize heartDictionary; 在 .m 文件中 @synthesize 它。
再说一次,除非你需要,否则我不会使用单例。将其作为变量传递给控制器。
There's no good reason not to just pass the dictionary along to your controller as a reference. If you create an NSMutableDictionary ivar in your OverviewController making it a property, you can then set the dictionary there when you create your controller or when it gets thawed from the nib.
Singletons are useful, but I wouldn't resort to it unless you really need it. You can change your -applicationDidFinishLaunching to something like this:
This assumes your rootController is of type OverviewController. Then in your OverviewController header you should declare a property like this:
@property (assign) NSMutableDictionary *heartDictionary;
and then @synthesize it in the .m file with @synthesize heartDictionary;.
Again, I wouldn't use a Singleton unless you need it. Pass it as a variable to your controller instead.