【问题标题】:NSMutableDictionary Being accessed from different classesNSMutableDictionary 被不同的类访问
【发布时间】:2009-11-11 20:18:06
【问题描述】:

在我的 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 文件中初始化和设置字典一次,然后从其他类访问该字典以更改其中的内容。有没有办法做到这一点?

【问题讨论】:

    标签: iphone objective-c nsmutabledictionary


    【解决方案1】:

    将你的 NSMutableDictionary 实例设为静态并编写一个类方法来访问它。把它放在你的 SheepAppDelegate.m 中:

    static NSMutableDictionary *theHeart;
    + (NSMutableDictionary*)theHeart
    {
        if (theHeart == nil) theHeart = [[NSMutableDictionary alloc] init];
    
        return theHeart;
    }
    

    并在其他任何地方使用:

    NSMutableDictionary *dict = [SheepAppDelegate theHeart];
    

    【讨论】:

      【解决方案2】:

      您可以将它放在您的 AppDelegate 中,也可以创建一个 Singleton。 This article 涵盖了这个主题和许多可能的选项,包括我提到的两个。

      单身人士似乎是更有条理的方法。您可以将所有全局信息存储在一个文件中,并且可以从任何地方访问它。

      【讨论】:

        【解决方案3】:

        没有充分的理由不将字典作为参考传递给您的控制器。如果您在 OverviewController 中创建 NSMutableDictionary ivar 使其成为属性,则可以在创建控制器或从笔尖解冻时将字典设置在那里。

        单身人士很有用,但除非你真的需要,否则我不会求助于它。您可以将 -applicationDidFinishLaunching 更改为以下内容:

        - (void)applicationDidFinishLaunching:(UIApplication *)application {    
        
            //Set the values for the Variables and Constants, these are
            //accessed from the different classes.
        
            NSMutableDictionary *theHeart = [NSMutableDictionary dictionary];
        
            [theHeart setObject:[NSNumber numberWithInt:200] forKey:@"Money"];
            [theHeart setObject:@"Number two!" forKey:@"2"];
        
            [rootController setHeartDictionary:theHeart];
        
            [window addSubview:rootController.view];
            [window makeKeyAndVisible];
        }
        

        这假设你的 rootController 是 OverviewController 类型。然后在您的 OverviewController 标头中,您应该声明如下属性:

        @property (assign) NSMutableDictionary *heartDictionary;

        然后用@synthesize heartDictionary;在.m文件中@synthesize它。

        同样,除非您需要,否则我不会使用 Singleton。而是将其作为变量传递给您的控制器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-25
          相关资源
          最近更新 更多