Question

So I've declared this in my appDelegate.h

       @property(nonatomic,strong) NSMutableArray *featured;

I've synthesized it like so in my appDelegate.m

       @synthesize featured;

When I log that out in the appDelegate with the object stored in there, I get the value it's supposed to have

In a viewController.h file I have declared this

       @property(nonatomic,strong) NSMutableArray *featured;

In the viewController.m file I've synthesized it like this

       @synthesize featured;

I then print out this line and get a null value

       NSLog(@"HERE %@", featured);

That same line prints out the correct value in my appDelegate.m file. I'm completely lost. I've set it up in the way I've done it for a previous class exercise. Thanks in advance!

Edit:

I created the array in appDelegate.m file like so in a method I called loadFeatured

  featured = [NSMutableArray array];

for (id dict in tempArray)
{
    //NSLog(@"dict=%@",dict);

    NSString *shopName = [dict objectForKey:@"shopName"];
    NSString *drinkName = [dict objectForKey:@"drinkName"];
    NSNumber *likes = [dict objectForKey:@"likes"];
    NSNumber *dislikes = [dict objectForKey:@"dislikes"];
    NSString *review = [dict objectForKey:@"review"];        

    Featured *feat = [[Featured alloc] initWithName:shopName drinkName:drinkName likes:likes dislikes:dislikes review:review];
    NSLog(@"feat=%@\n\n",feat);
    [featured addObject:feat];
}

NSLog(@"there is %d featured",[featured count]);
NSLog(@"HERE %@", featured);
Was it helpful?

Solution

Here is the way, how to access the data stored in the app delegate from your viewcontroller.

You need not synthesize the object in the viewcontroller. Just import your appdelegate file and copy the following code wherever necessary.

NSMutableArray * nArray =[ (AppDelegate*) [[UIApplication sharedApplication] delegate] featured];

The above code gives you the required array from the app delegate.Now you can make use of the nArray object to display the details in the console.

NSLog(@"%@",nArray.description);

OTHER TIPS

It's hard to say how to do this without knowing the structure of your app. You could pass a pointer to the array to your view controller, if you have access to that view controller from the app delegate. The other way is to get a reference to the app delegate in your view controller, and then access its array. That can be done like this:

AppDelegate *appDel = [UIApplication shared application].delegate;
NSArray *myControllerArray = appDel.featured;

You'll need to import your app delegate into your controller's .m file to use this approach.

Since you already declared a property in appDelegate.h you can access it in the other viewController like this:

#import "appDelegate.h"

and you can access the value it by using something like this:

((AppDelegate *)[[UIApplication sharedApplication]delegate]).featured

If you need to access an NSArray or any other object in any class, via AppDelegate, just create a property to access your ViewController, like so, in your AppDelegate class:

#import "ViewController.h"

@property (nonatomic, strong) AppDelegate *appDelegate;
@property (nonatomic, strong) ViewController *viewController; 

In your ViewController class:

#import "AppDelegate.h"

AppDelegate    *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
ViewController *viewControllerREFERENCE = [appDelegate viewController];

Then you'll have access to any value on your ViewController, via AppDelegate. I hope that helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top