• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
debugging hard-to-locate error in NSApplication delegate
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

debugging hard-to-locate error in NSApplication delegate


  • Subject: debugging hard-to-locate error in NSApplication delegate
  • From: Greg Beaver <email@hidden>
  • Date: Mon, 12 Jan 2009 10:11:19 -0600

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@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: debugging hard-to-locate error in NSApplication delegate
      • From: Quincey Morris <email@hidden>
    • Re: debugging hard-to-locate error in NSApplication delegate
      • From: "Michael Ash" <email@hidden>
  • Prev by Date: Re: Mac Pro memory sizes
  • Next by Date: Re: create custom control same as multiple monitor control that manages monitors windows
  • Previous by thread: Re: Security With Show Package Contents?
  • Next by thread: Re: debugging hard-to-locate error in NSApplication delegate
  • Index(es):
    • Date
    • Thread