1

I am working with the document architecture in Cocoa but will create my own window instead of a nib. I am replacing NSApplicationMain with code that has worked for apps not using the document architecture.

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSApplication *app = [[NSApplication alloc] init];
        AppDelegate *appDelegate = [[AppDelegate alloc] init];
        [app setDelegate:appDelegate];
        [app run];
    }
}

But with the document architecture, when I create a window (I know this is not the way to create one, but for simplicity) ...

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    NSWindow *window = [[NSWindow alloc] init];
}

... I receive the error below.

*** Assertion failure in -[NSApplication init], /SourceCache/AppKit/AppKit-1344.72/AppKit.subproj/NSApplication.m:1787

I have read the entire guide on the document architecture, tried creating the window many places, and have taken careful consideration to fit the procedure that the architecture supports, as the image with link below shows.

https://i.stack.imgur.com/RR3nK.png

Everything I've tried leads to the error above whenever I create a window, regardless of where I create it. One possible source of error is that I begin the document creation process with the OpenUntitledDocumentAndDisplay:error: inside of my appDelegate's applicationDidFinishLaunching:notification method in which case the NSApplication might view this as creating a document too soon.

In brief, why does creating a window object in the document architecture result in an NSApplication error, specifically that I'm creating more than one application?

Joseph Johnston
  • 533
  • 1
  • 3
  • 14
  • The code snippet seems far at odds with the png image you link to-- allocating a bare window absent the NSWindowController, NSDocument, and NSDocumentController. But you do say that. So I'll ask, do you override the defaultType on your NSDocumentController subclass? Do you override makeWindowControllers on your NSDocument subclass? If you did, why do you need to explicitly create a window controller? You should get one from makeWindowControllers. – stevesliva Mar 31 '15 at 05:31
  • Hard to tell w/o seeing your code but I'd guess you've destroyed some other part of the `NSAppplication` logic and the window issue is just a red herring. – cacau Mar 31 '15 at 06:01
  • I don't see a reason to override defaultType. The correct type is read from info.plist. Yes, I override makeWindowControllers (but not in the simple example above) and inside that method manually create a window. Its when I create that window that it complains. I rewrote the app in a fresh template but got same error. – Joseph Johnston Apr 01 '15 at 01:06
  • @JosephJohnston - as long as the plist type is loaded. I know that by not using NSApplicationMain, you lose some of the plist settings. Like the main nib file. – stevesliva Apr 01 '15 at 03:11

1 Answers1

2

Looking at my own code for doing without NSApplicationMain(), you don't alloc init the NSApplication instance.

You should do this-- use the sharedApplication singleton generator method:

NSApplication *application = [NSApplication sharedApplication];

See, for instance, the answer here. I also talk about a few other things that break when NSApplicationMain() isn't used.

@cacau got me squinting at that assertion. It's in the NSApplication init method you call in the very first line of the app. Does it look like the exception happens there if you set an exception breakpoint?

Hope that helps, though I expect you'll run into more issues. I haven't done the no-NSApplicationMain thing for a document based app.

For what it's worth, the sharedApplication reference says:

This method also makes a connection to the window server and completes other initialization. Your program should invoke this method as one of the first statements in main(); this invoking is done for you if you create your application with Xcode.

Community
  • 1
  • 1
stevesliva
  • 5,351
  • 1
  • 16
  • 39
  • That was it! Thank you so much. I did the correct sharedApplication with non document apps but when I wrote this one I mindlessly created the app with alloc init. sharedApplication's connection to the window server is probably what caused the window error. Do you have any idea why the method sharedApplication has the word 'shared'? – Joseph Johnston Apr 01 '15 at 03:50
  • ["The name of the factory method returning the singleton instance has, by convention, the form sharedClassType. Examples from the Cocoa frameworks are sharedFileManager, sharedColorPanel, and sharedWorkspace."](https://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/Singleton.html) So it's just old cocoa singleton nomenclature. – stevesliva Apr 01 '15 at 04:44
  • Yes, so no evident reason. – Joseph Johnston Apr 01 '15 at 20:01