我在哪里为iOS应用程序创建全局变量?

时间:2012-03-06 23:30:09

标签: objective-c ios global-variables

这是我的代码:

我希望能够创建一个全局的NSMutableArray,它可以存储Budget *对象,然后可以将其写入.pList文件......我只是在学习pLists是什么,而且我有点朦胧如何实施它们......

我在哪里错了?

- (IBAction)btnCreateBudget:(id)sender 
{
    Budget *budget = [[Budget alloc] init];

    budget.name = self.txtFldBudgetName.text;
    budget.amount = [self.txtFldBudgetAmount.text intValue];    

    // Write the data to the pList
    NSMutableArray *anArray = [[NSMutableArray alloc] init]; // I want this to be a global variable for the entire app. Where do I put this?

    [anArray addObject:budget];

    [anArray writeToFile:[self dataFilePath] atomically:YES];

    /* As you can see, below is where I test the code. Unfortunately, 
    every time I run this, I get only 1 element in the array. I'm assuming 
    that this is because everytime the button is pressed, I create a brand new 
    NSMutableArray *anArray. I want that to be global for the entire app. */

    int i = 0;
    for (Budget * b in anArray)
    {
        i++;
    }
    NSLog(@"There are %d items in anArray",i);

}

-(NSString *) dataFilePath
{ 
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirectory = [path objectAtIndex:0]; 
    return [documentDirectory stringByAppendingPathComponent:@"BudgetData.plist"];
}

编辑:我想补充说我正在创建一个anArray数组,以便其他视图可以访问它。我知道这可以通过NSNotification完成吗?或者我应该这样做appDelegate类?最终目标是让anArray对象填充位于单独视图中的UITableView。

4 个答案:

答案 0 :(得分:3)

只需将声明放在方法之外而不是放在其中。

NSMutableArray *anArray = nil;

- (IBAction)btnCreateBudget:(id)sender 
{
    ...
    if ( anArray == nil )
      anArray = [[NSMutableArray alloc] init]; 
    ...
}

如果仅在一个文件中使用,请将其设置为“静态”,以防止与其他文件发生名称冲突:

    static NSMutableArray *anArray = nil;

如果它仅在一个方法中使用,则将其设为“静态”并将其放入该方法中:

- (IBAction)btnCreateBudget:(id)sender 
{
    static NSMutableArray *anArray = nil;
    ...
    if ( anArray == nil )
      anArray = [[NSMutableArray alloc] init]; 
    ...
}

请注意,人们通常会对全局变量使用某种命名约定,例如“gArray”,以便轻松地将它们与局部变量,实例变量或方法参数区分开来。

答案 1 :(得分:2)

在这种情况下,不需要全局变量。你可以这样做:

  1. 将旧数据读取到可变数组(initWithContentsOfFile:)。
  2. 向阵列添加新记录。
  3. 将数组保存到同一文件。
  4. 但是代码中的第二个问题是,如果您的Budget类不是属性列表类型(NSString,NSData,NSArray或NSDictionary对象),writeToFile:将无法成功保存它。

答案 2 :(得分:1)

您需要确保预算类调用NSCoder,然后调用NSCoder initWithCoder:NSCoder decodeWithCoder:方法。否则,writeToFile:将无效NSObject课程。

但我离题了。原始问题的答案应如下:

.h文件中,您需要执行以下操作。

@interface WhateverClassName : UIViewController 
{
    NSMutableArray *anArray;

}

@property(nonatomic, retain) NSMutableArray *anArray;
@end

然后,您需要确保@synthesize NSMutableArray,以便您不会收到任何怪异的警告。这是在@implementation文件中的.m行之后完成的。

然后,在您希望将其分配到内存的函数中,只需执行以下操作。

anArray = [[NSMutableArray alloc] initWithObjects:nil];

现在这是一个global变量。它是global,因为它可以在任何函数中使用,并且不限于在一个函数中使用。

答案 3 :(得分:0)

如果您希望整个应用程序或上下文(“全局”)可以访问数据,则可以使用单例。但是,要小心这样做,并确保它实际上是必要和适当的。在任何单例实现之前,我建议大量阅读它。卡特艾伦有一个很好的基本实施here

根据“最终目标是让anArray对象填充单独视图中的UITableView”,您不需要向文件,数据库或单例写入任何内容。只需设置对象。如Sebastien Peek所述。

如果您希望离线数据存储,请查看sqlite,json,plist等

相关问题