Skip navigation.
 
mldebugging hard-to-locate error in NSApplication delegate
FROM : Greg Beaver
DATE : Mon Jan 12 17:11:19 2009

Hi,

Does anyone have working code that shows an application delegate in a
core data document-based app?  I'm trying to intercept
applicationShouldOpenUntitledFile: in order to open the last-saved
document.  I have code that works, but it causes this error:

*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil

some time between the setting of the delegate, which I did by inserting
this object into the nib and setting the delegate programmatically.
linking from IB did not properly set the delegate (init and
applicationDidFinishLaunching: were called, but
applicationShouldOpenUntitledFile: was never called):

@implementation GreenwoodAppDelegate

- (void) init
{
    [super init];
    applicationHasStarted = NO;
    if (![[NSApplication sharedApplication] delegate]) {
        [[NSApplication sharedApplication] setDelegate:self];
    }
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    applicationHasStarted = YES;
}

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    // On startup, when asked to open an untitled file, open the last opened
    // file instead
    if (!applicationHasStarted)
    {
        // Get the recent documents
        NSDocumentController *controller =
        [NSDocumentController sharedDocumentController];
        NSArray *documents = [controller recentDocumentURLs];
     
        // If there is a recent document, try to open it.
        if ([documents count] > 0)
        {
            NSError *error = nil;
            [controller
            openDocumentWithContentsOfURL:[documents objectAtIndex:0]
            display:YES error:&error];
         
            // If there was no error, then prevent untitled from appearing.
            if (error == nil)
            {
                return NO;
            }
        }
    }
 
    return YES;
}
@end

I borrowed the code in applicationShouldOpenUntitledFile: from a helpful
blog entry, can't remember where.

Thanks,
Greg

Related mailsAuthorDate
mldebugging hard-to-locate error in NSApplication delegate Greg Beaver Jan 12, 17:11
mlRe: debugging hard-to-locate error in NSApplication delegate Michael Ash Jan 12, 17:24
mlRE: debugging hard-to-locate error in NSApplication delegate Jon C. Munson II Jan 12, 17:42
mlRe: debugging hard-to-locate error in NSApplication delegate Quincey Morris Jan 12, 19:41
ml[SOLVED] Re: debugging hard-to-locate error in NSApplication delegate Greg Beaver Jan 12, 19:54
mlRe: debugging hard-to-locate error in NSApplication delegate Keary Suska Jan 13, 01:40
mlRe: debugging hard-to-locate error in NSApplication delegate Michael Ash Jan 13, 02:06
mlRe: debugging hard-to-locate error in NSApplication delegate mmalc Crawford Jan 13, 02:16
mlRe: debugging hard-to-locate error in NSApplication delegate Quincey Morris Jan 13, 02:47