gpt4 book ai didi

swift - 在 ConstantsToExport 中导出 swift 枚举

转载 作者:行者123 更新时间:2023-11-30 10:44:50 25 4
gpt4 key购买 nike

在我的 React-native Swift native 模块中,我尝试将 Swift 枚举导出到 javascript:

@objc(InterfaceOrientationManager)
class InterfaceOrientationManager: NSObject {

enum InterfaceOrientation: String {
case landscapeRight
case landscapeLeft
case portrait
case portraitUpsideDown
}

@objc
static func requiresMainQueueSetup() -> Bool {
return true
}

@objc
func constantsToExport() -> [String: Any]! {
return [
"InterfaceOrientation": InterfaceOrientation
]
}

但是这不起作用,我收到错误“类型名称后预期的成员名称或构造函数调用”。屏幕截图位于底部。

有什么办法可以实现这一点吗?我希望避免编写字典:

 let InterfaceOrientation: [String: String] = [
"landscapeRight": "landscapeRight",
"landscapeLeft": "landscapeLeft",
"portrait": "portrait",
"portraitUpsideDown": "portraitUpsideDown"
]

enter image description here

最佳答案

从枚举创建一个字典,如下所示:

enum InterfaceOrientation: CaseIterable {
case landscapeRight, landscapeLeft, portrait, portraitUpsideDown
}

var interfaceDictionary: [InterfaceOrientation: String] = [:]
for orientation in InterfaceOrientation.allCases {
interfaceDictionary[orientation] = "\(orientation)"
}

关于swift - 在 ConstantsToExport 中导出 swift 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55957169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com