且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-24 23:51:04

只需将声明放在方法而不是内部。

  NSMutableArray * anArray = nil; 

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

如果仅在一个文件中使用它,使其成为静态来防止与其他文件的名称冲突:

  static NSMutableArray * anArray = nil; 

如果仅在一个方法中使用它,请将其设置为static并将其放入该方法中:

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

请注意,人们通常会使用某种命名全局变量的约定,比如gArray,可以很容易地将它们与局部变量,实例变量或方法参数区分开来。

Here is my code:

I want to be able to create a global NSMutableArray that can store Budget* objects that can then be written to a .pList file... I'm only learning what pLists are, and I am a bit hazy about how to implement them...

Where am I going wrong here?

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

edit: I'd like to add that I am creating the anArray array so that it can be accessible by other views. I understand that this can be done with NSNotification? or Should I do this the appDelegate classes? The end goal is to have the anArray object populate a UITableView that is in a separate View.

Just put the declaration outside the method instead of inside it.

NSMutableArray *anArray = nil;

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

If it's only used inside the one file, make it "static" instead to prevent name collisions with other files:

    static NSMutableArray *anArray = nil;

If it's only used inside the one method, make it "static" and put it inside that method:

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

Note that people usually use some kind of naming convention for global variables, like "gArray", to easily differentiate them from local variables, instance variables, or method parameters.