Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere do I create global variables for an iOS app?
    text
    copied!<p>Here is my code:</p> <p>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...</p> <p>Where am I going wrong here?</p> <pre><code>- (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"]; } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload