NSApplication init失败:创建多个应用程序 [英] NSApplication init failure: Creating more than one Application

查看:86
本文介绍了NSApplication init失败:创建多个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cocoa中的文档体系结构,但是将创建自己的窗口而不是笔尖。我将NSApplicationMain替换为使用文档体系结构对应用程序 not 无效的代码。

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.

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

我尝试过的所有方法都会导致无论何时创建窗口,无论在何处创建,都会出现上述错误。错误的可能来源之一是,我在appDelegate的 applicationDidFinishLaunching:notification OpenUntitledDocumentAndDisplay:error:开始文档创建过程。 c>在这种情况下,NSApplication可能认为这太早创建了文档。

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.

总之,为什么在文档体系结构中创建窗口对象会导致NSApplication错误,特别是我要创建多个应用程序?

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?

推荐答案

看我自己的代码,不用 NSApplicationMain(),您无需 alloc init NSApplication 实例。

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

您应该执行此操作-使用 sharedApplication 单例生成器方法:

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

NSApplication *application = [NSApplication sharedApplication];

例如,参见此处的答案。我还谈论了其他一些事情,这些事情在不使用 NSApplicationMain()时会中断。

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

@cacau得到了我at着那个断言。它位于您在应用程序第一行中调用的 NSApplication init 方法中。如果您设置了异常断点,看起来异常发生在那里吗?

@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?

希望有帮助,尽管我希望您会遇到更多问题。对于基于文档的应用程序,我还没有做过noNSApplicationMain事情。

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.

对于它的价值, sharedApplication 参考文献说:

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


此方法还建立与窗口服务器的连接并完成其他初始化。您的程序应将此方法作为main()中的第一条语句之一来调用;如果使用Xcode创建应用程序,则此调用已为您完成。

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.

这篇关于NSApplication init失败:创建多个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆