标识应用程序是否由于系统正在关闭而终止

问题描述 投票:0回答:1

我正在尝试将Objective-C中的这些行转换为快速行

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender  
{  
   ...  
    NSAppleEventDescriptor* appleEventDesc = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent];  
    NSAppleEventDescriptor* whyDesc = [appleEventDesc attributeDescriptorForKeyword:kEventParamReason];  
    OSType why = [whyDesc typeCodeValue];  
    if (why==kAEShutDown || why==kAERestart || why==kAEReallyLogOut)  

它们将检测应用是否由于系统关闭而终止。

我无法转换此行

if (why==kAEShutDown || why==kAERestart || why==kAEReallyLogOut)  

我猜是这样的

    let codeValue = whyDescription?.typeCodeValue

if ((codeValue == AEKeyword(kAEShutDown)) ||
    (codeValue == AEKeyword(kAERestart)) ||
    (codeValue == AEKeyword(kAEReallyLogOut))) {

这是正确的吗?

或者应该是

if ((codeValue == OSType(kAEShutDown)) ||
    (codeValue == OSType(kAERestart)) ||
    (codeValue == OSType(kAEReallyLogOut))) {

Xcode可以很好地进行编译,但是我不确定kAEShutDownkAERestartkAEReallyLogOut是否是可在此处使用的AEKeywords。

按预期,我找不到任何文档。

objective-c swift macos cocoa shutdown
1个回答
1
投票

AEKeywordOSType均为typealiasFourCharCode。从技术上讲,它们是相同的类型,因此使用哪个都无所谓。

但是,由于将typeCodeValue声明为OSType,因此OSType是逻辑选择。

而且,似乎常量已经声明为OSType,因此应该没有任何理由要强制转换它们。

© www.soinside.com 2019 - 2024. All rights reserved.