Attempt to access application delegate variables / properties

I use the variable / property of my application delegate as global. (I do not want to deal with a singleton class.)

I am trying to write a #define statement in the Application Delegate class. If I print:

  [UIApplication sharedApplication]

in my application delegate class, the code hint does not recognize sharedApplication. But if I find the same thing in the viewController class, "sharedApplication" appears correctly.

To define an NSMutableDictionary in my Application Delegate.h (or .m?), I write:

#define MyDictionary [[[UIApplication sharedApplication] delegate] stateDictionary]

Then, if I try to use it in another class:

[[MyDictionary objectForKey:@"California"] largestCity];

I get a message that MyDictionary should be declared first. I am really confused by many ideas here.

+3
2

, - , :

, myApplication. "" MyApplicationDelegate, :

// get reference to app delegate
MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate]

Property *myProperty = appDelegate.property;

, , MyApplicationDelegate :

#import "MyApplicationDelegate.h"

, "" - , .

+8

UIApplication, :

@interface UIApplication (MyAppDelegate)
+(MyAppDelegate*)sharedMyAppDelegate;
@end

, . , MyAppDelegate. , #import. MyProject _Prefix.chp.

, ( , ).

+2

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


All Articles