Where can I create global variables for an iOS application?

Here is my code:

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

Where am I 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 would like to add that I am creating an anArray so that it can be accessed by other views. I understand that this can be done using NSNotification? or should I do this with the appDelegate classes? The ultimate goal is for the anArray to populate the UITableView, which is in a separate view.

+1
source share
4 answers

Just place the declaration outside the method, not inside it.

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

If it is used only inside one file, make it โ€œstaticโ€ to prevent name conflicts with other files:

  static NSMutableArray *anArray = nil; 

If it is used only inside one method, make it โ€œstaticโ€ and place it inside this 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 such as "gArray" to easily distinguish them from local variables, instance variables, or method parameters.

+3
source

In this case, a global variable is not needed. You can do something like this:

  • Read the old data in a mutable array ( initWithContentsOfFile: .
  • Add a new entry to the array.
  • Save the array to the same file.

But the second problem in your code is that if your budget class is not a type of property list (NSString, NSData, NSArray or NSDictionary objects) writeToFile: will not save it successfully.

+2
source

You need to make sure your budget class calls NSCoder , and then the NSCoder initWithCoder: and NSCoder decodeWithCoder: . Otherwise, writeToFile: will not work for you NSObject .

But I was distracted. The answer to the original question should be as follows.

In your .h file, you need to do the following.

 @interface WhateverClassName : UIViewController { NSMutableArray *anArray; } @property(nonatomic, retain) NSMutableArray *anArray; @end 

Then you need to make sure that you are @synthesize NSMutableArray so that you don't have any fancy warnings. This is done immediately after the @implementation line in your .m file.

Then, within the function that you want to allocate to memory, simply follow these steps.

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

Now this is a global variable. It is global in the sense that it can be used from any function and is not limited to being used in one function.

+1
source

If you want the data to be accessible for the entire application or context ("global"), you can use a singleton. However, do this with caution and make sure that it is truly necessary and appropriate. I would suggest doing a lot of reading on it before any singleton implementation. Carter Allen has a good basic implementation here .

According to "The ultimate goal is to include an anArray in a UITableView, which is in a separate view," you do not need to write anything to a file, database or singleton. Just set the object. As stated by Sebastian Peak.

If you want offline data storage, take a look at sqlite, json, plist, etc.

0
source

Source: https://habr.com/ru/post/920824/


All Articles