如何使[NsApp运行]不被阻止? [英] How to make [NsApp run] not block?

查看:107
本文介绍了如何使[NsApp运行]不被阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是可可的新手.

现在,从main()调用[NsApp run]之后,我的简单Hello World应用程序即会阻塞.

Right now my simple Hello World app blocks after calling [NsApp run] from main().

我所需要做的就是创建一个窗口,而不是阻塞main().

All I need is create a window and not block main().

我希望我的应用程序的行为类似于glfw:

I want my application to behave like glfw:

https://github.com/glfw/glfw/blob/master/src/cocoa_window.m#L1022

由于某种原因,它不会在那里阻塞.实际上,您可以删除此行,它仍然可以使用.

For some reason, it doesn't block there. In fact, you can remove this line, and it will still work.

我一直在和glfw的源代码打交道,以弄清它们的不同之处.例如,如果我删除[NSApp setDelegate:_glfw.ns.delegate];

I've been playing with a glfw source to figure out what they do differently. And for example, [NsApp run] blocks if I remove [NSApp setDelegate:_glfw.ns.delegate];

但这不是.

根据 Apple文档:

NSApplication类在安装过程中设置@autorelease块 初始化和事件循环内-特别是在事件循环内 初始化(或共享)和run()方法.

The NSApplication class sets up @autorelease block during initialization and inside the event loop—specifically, within its initialization (or shared) and run() methods.

通常,应用程序会在事件循环发生时创建对象 运行或通过从nib文件加载对象,因此缺少访问权限 通常不是问题.但是,如果您确实需要使用Cocoa类 在main()函数本身中(除了加载nib文件或 实例化NSApplication),则应创建一个@autorelease块以 使用这些类来包含代码.

Typically, an app creates objects either while the event loop is running or by loading objects from nib files, so this lack of access usually isn’t a problem. However, if you do need to use Cocoa classes within the main() function itself (other than to load nib files or to instantiate NSApplication), you should create an @autorelease block to contain the code using the classes.

我想这就是我所需要的,但是我不知道如何使用@autorelease块.

I guess that's what I need, but I have no idea how to use the @autorelease block.

感谢您的帮助.

推荐答案

我知道了.

GLFW实现了自己的事件循环,因此不需要调用[NSApp run]:

GLFW implements its own event loop, so calling [NSApp run] is not needed:

NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny
                                        untilDate:[NSDate distantFuture]
                                           inMode:NSDefaultRunLoopMode
                                          dequeue:YES];
    [NSApp sendEvent:event];

这篇关于如何使[NsApp运行]不被阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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