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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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 : galloway.me.uk/tutorials/singleton-classes. You will find tons of examples on google as well
I see thanks, I will however at some point have more than one instance as I filter based on two values
Hi, I did a little more searching and I've tried what has been mentioned here stackoverflow.com/questions/19995395/… 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?
Brilliant! I've set up a singleton class and it works a treat thanks!
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.