Reputation: 936
I have a disappearing pointer db; the value is properly set during creation of an NSDocument
but at the moment I want to open a sub window, the value has changed to nil
! I have the following in an NSDocument
subclass:
@interface MW_Document : NSDocument
{
MW_WorkerWindowController *workerController;
__strong MW_db *db;
}
- (IBAction)showWorkerManagementPanel:(id)sender;
//- (IBAction)showSkillManagementPanel:(id)sender;
The implementation contains this:
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
if (![self db]) {
db = [[MW_db alloc] init];
NSLog ( @"Debug - Init of db: [%ld]", db ); // never mind the casting problem
}
}
db points at something other than nil, a true address.
Later on, I want to open a window and have this in the implementation of the same NSDocument
subclass:
- (IBAction)showWorkerManagementWindow:(id)sender
{
if ( !workerController) {
workerController = [[MW_WorkerWindowController alloc] initWithDb:db];
}
[workerController showWindow:self];
}
I put a break point at the first line, and look at the value of db. It is nil, but I have no idea why. Can anyone explain this to me?
Upvotes: 1
Views: 110
Reputation: 9493
You can implement a lazy accessor:
- (MW_db *)db
{
if (db == nil) {
db = [[MW_db alloc] init];
}
return db;
}
And then use it instead of the ivar:
workerController = [[MW_WorkerWindowController alloc] initWithDb:[self db]];
Upvotes: 2