I tried to made my AppDelegate as a singleton and access it through my application like :
AppDelegate.h
/.../
@interface AppDelegate : UIResponder <UIApplicationDelegate>
+(AppDelegate*)sharedAppDelegate;
@end
AppDelegate.m
#import AppDelegate.h
/.../
@implementation AppDelegate
AppDelegate *sharedAppDelegate;
+ (AppDelegate *)sharedAppDelegate{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedAppDelegate = [[self alloc] init];
});
NSLog(@"shared app: %@",sharedAppDelegate)
return sharedAppDelegate;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"launched app: %@",self);
}
MyClass.m
#import AppDelegate.h
/.../
- (void)viewDidLoad{
[super viewDidLoad];
NSLog(@"app in myClass: %@",[AppDelegate sharedAppDelegate]);
}
Logs in console:
[***]launched app: <AppDelegate: 0x78757810>
[***]shared app: <AppDelegate: 0x78f39760>
[***]app in myClass: <AppDelegate: 0x78f39760>
Why isn't the launched one the same as the shared one?
Am I not really make AppDelegate a singleton?