我正在尝试将 Objective-C 中的这些行转换为 swift
- (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 编译得很好,但我不确定 kAEShutDown
、kAERestart
和 kAEReallyLogOut
是否是可以在那里使用的“AEKeywords” .
正如预期的那样,我找不到任何相关文档。
最佳答案
AEKeyword
和 OSType
都是 FourCharCode
的 typealias
。从技术上讲,它们是相同的类型,因此您使用哪一个并不重要。
但是,由于 typeCodeValue
被声明为 OSType
,因此 OSType
是合乎逻辑的选择。
此外,似乎常量已声明为 OSType
,因此不应该有任何理由强制转换它们。
关于objective-c - 确定应用程序是否因系统关闭而终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60326153/