I need to initialize an object of an object in my AppDelegate at startup. Whenever I try to access the ivar somewhere else all that is returned is null.
What I’m doing can be simplified as
//AppDelegate.h:
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
NSString* someString;
}
@property (nonatomic, copy) NSString* someString;
and then in the AppDelegate.m
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
someString = @"Hello World!";
NSLog(@"%@",someString);
}
This is what I do when I access the NSString-object
//ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%@",delegate.someString);
}
The output text is
- 2012-09-30 22:26:45.125 Labor3[22524:f803] Hello World!
- 2012-09-30 22:26:45.127 Labor3[22524:f803] (null)
without any warnings or errors.
The problem I’m having is a bit more complicated but the essentials stay the same and I can’t even get the above example to work. Any hint is appreciated.
Add
After
in your app-delegate .m file.