Reputation: 543
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.sstatic.net/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?
Upvotes: 2
Views: 2005
Reputation: 5655
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.
Upvotes: 2