objective-c - 如何让[NsApp run]不被阻塞?

标签 objective-c macos cocoa

我是一个彻头彻尾的 Cocoa 新手。

现在,我的简单 Hello World 应用程序在从 main() 调用 [NsApp run] 后被阻止。

我需要的只是创建一个窗口而不是阻止 main()。

我希望我的应用程序表现得像 glfw:

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

出于某种原因,它不会阻塞在那里。事实上,你可以删除这一行,它仍然可以工作。

我一直在研究 glfw 源来找出它们的不同之处。例如,如果我删除 [NSApp setDelegate:_glfw.ns.delegate];

[NsApp run] 会阻止

但事实并非如此。

根据Apple docs :

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

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 block 。

感谢您的帮助。

最佳答案

我明白了。

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

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

关于objective-c - 如何让[NsApp run]不被阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48020222/

相关文章:

macos - 在 Mac OS X 中,如何获得文件描述符使用的准确计数?

cocoa - 事件的发生日期与开始日期有何不同?

objective-c - AutoreleasePool 没有捕获断点,没有警告,Instruments Leaks 中没有报告

objective-c - Obj-C 中的乘法问题

ios - 如何处理 UICollectionView 中的后台点击

windows - 如何在 Mac 工作站和 Windows 虚拟机之间共享本地 Subversion 存储库?

macos - 映射大写字母仅在 vim 中锁定?

cocoa - 如何在 NSTableCellView 子类中接收鼠标点击?

objective-c - 在应用程序或运行循环之外使用 Grand Central Dispatch

objective-c - 以编程方式使用 BWToolkit