0

I am trying to create a global array in my app which can be accessed by ALL view controllers. I have put the code in the AppDelegate and then imported the AppDelegate in to the view controller files.

Within the app delegate, I have created an array in the didFinishLaunchingWithOptions: function. However, I need to create a new function in the AppDelegate which I can then call from other view controllers to filter this global array. So far I have this:

// Function to get suggested foods
- (BOOL)getFood:inCategory(NSString *)foodCat 
{
NSMutableArray *filteredArray=[[foodArrayMain filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(foodCat==%@)",foodCat]] mutableCopy];

return filteredArray;
}

However, I know there are errors here and it doesn't seem to work. I have tried calling it in one of the other view controllers like this:

NSArray *foods= [AppDelegate getFood:@"meats"];

Any help?

Prateek
  • 1,724
  • 3
  • 15
  • 27

2 Answers2

1

You need to use the AppDelegate instance rather than the AppDelegate class (since your method is an instance method) :

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedInstance].delegate;
NSArray *foods = [appDelegate getFood:@"meats"];

As Amandir mentioned change the return value of your method to NSArray * since that is the type of the value you are returning from this method. (Or use NSMutableArray * if you intend to modify the array by the calling class or anywhere else)

A more suitable approach will be to define a singleton class which manages your global settings/data.

giorashc
  • 13,691
  • 3
  • 35
  • 71
  • Thanks, I see. Could you explain this singleton class a little more? It's just I have an array with over 200 values in and I think repeating this array in 4-5 view controllers, although simple to do, would be inefficient and take up unnecessary speed. – Prateek Jul 20 '14 at 10:45
  • A singleton is a class which permits only one instance and is accessible (the shared instance that is) from anywhere in your code. Take a look here : http://www.galloway.me.uk/tutorials/singleton-classes/. You will find tons of examples on google as well – giorashc Jul 20 '14 at 10:47
  • I see thanks, I will however at some point have more than one instance as I filter based on two values – Prateek Jul 20 '14 at 10:48
  • Hi, I did a little more searching and I've tried what has been mentioned here http://stackoverflow.com/questions/19995395/how-and-where-do-i-intialize-an-global-nsmutablearray-in-xcode-5 in Smita's answer. I've use AppDelegate and it seems to work fine. Now I can just filter the array on the viewcontrollers with local functions as and when needed. Is this an incorrect way of doing things/inefficient way? – Prateek Jul 20 '14 at 11:01
  • Its no inefficient but the appdelegate purpose is to handle App related events. Both ways will work though with the same efficiency. – giorashc Jul 20 '14 at 11:05
  • 1
    Brilliant! I've set up a singleton class and it works a treat thanks! – Prateek Jul 20 '14 at 11:37
0

First step would be to change the ReturnValue from:

- (BOOL)getFood:inCategory(NSString *)foodCat

to:

- (NSMutableArray *)getFood:inCategory(NSString *)foodCat

Also: I wouldn't use AppDelegate for this. I is not really good style.

Another way would be to use a propertyList in your app's document folder and use that to serialise/deserialise the array information.

Amandir
  • 679
  • 1
  • 7
  • 20