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
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to