Pregunta

I'm very new to Objective C so please bear with me. I have been searching through a lot threads but still couldn't find suitable answer for my case yet I believe this question has been asked over and over. I found a lot of tutorials how to use AppDelegate for sharing string but I can't figure out how to use it to share NSMUtableDictionary.

I want to share one NSMutableDictionary between two classes where I add data to the NSMutableDictionary in one class and read it it another. I'm using AppDelegate class to store the data.

AppDelegate.h

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,retain) NSMutableDictionary *myArray;
@end

AppDelegate.m

#import "TestResults.h"
@synthesize myArray;

TestResults.h

@interface TestResults : UIViewController {
singletonObj * sobj;
}

TestResults.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSMutableDictionary *myArrayFromAppDelegate = appDelegate.myArray;
    [myArrayFromAppDelegate setValue:@"aaa" forKey:@"bbb"];
    NSLog(@"%@", myArrayFromAppDelegate);
}

When i do NSLog it return an empty array. Where did I go wrong?

¿Fue útil?

Solución

I think you forget to alloc and init in AppDelegate. In

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    myArray = [[NSMutableDictionary alloc]init];
    .
    .


    [self.window makeKeyAndVisible];
    return YES;
}

And you can give your Dictionary name : myDict it is benificial for you.

And by calling this myArray from other ViewController gives you empty NSMutableDictionary.

Otros consejos

Oh lordy. This is indeed a VERY common beginner question.

The real answer to the question "How do I share objects in the app delegate" is "dont". It clutters up your app delegate, and makes it do work it was not intended to do. It's like storing your food from your house in your car. It works, but it weighs down the car so it doesn't work as well at it's primary job.

You should design your apps with as little global state as possible.

If you do need to share global state data, don't put it int the app delegate. Instead, create a data container singleton and use that.

You should be able to do a search on [ios] singleton and find lots of examples of creating singletons. A data container singleton is just a singleton that has properties that are used to hold and share data.

Second point:

You have an NSMutableDictionary called myArray. That is a recipe for confusion. Don't use the name of another type (the wrong type) in naming your dictionary. DO NOT DO THS! EVER! If it's not an array, don't call it an array.

Third point:

As others have pointed out, you never alloc/init your dictionary. The alloc/init should take place in the object that owns the dictionary (in your case, AppDelegate, but move it to your data container singleton.) You can ether create it in the owning class's init method, or write a custom getter that "lazy loads" the dictionary:

- (NSDictionary *)myDict;
{
  if (!_myDict)
  {
    myDict = [NSMutableDictionary new];
  }
  return myDict;
}

Here you may alloc this dict.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   self.myArray = [NSMutableDictionary dictionary];
  .....
}

Otherwise you can do in TestResults.m

NSMutableDictionary *myArrayFromAppDelegate = appDelegate.myArray;
if (!myArrayFromAppDelegate)
    appDelegate.myArray = [NSMutableDictionary dictionary];

You can also do this work.

   - (void)viewDidLoad
    {
        [super viewDidLoad];
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.myDictionary=[[NSMutableDictionary alloc]init];////This could be another way
        NSMutableDictionary *myDictionaryFromAppDelegate = appDelegate.myDictionary;///Don't name an dictionary by array!!!!!!!
        [myDictionaryFromAppDelegate setValue:@"aaa" forKey:@"bbb"];
        NSLog(@"%@", myDictionaryFromAppDelegate);
    }

Thanks

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top