编辑: 我向你们所有人道歉,这一直是我的错 - 即使在 Swift 3 中也是不可能的。我很困惑自己。抱歉这个问题。
显然,在 Swift 4 中不再可能将枚举值数组桥接到 Objective C,甚至不使用 @objc 注释:
@objc open func removeCacheAfterDelay(_ delay: Double, forType types: [CacheManagerType]) {
}
@objc public enum CacheManagerType: Int, RawRepresentable {
case credit
case debit
case transactionHistory
}
removeCacheAfterDelay 函数显示以下错误:
Method cannot be marked @objc because the type of the parameter 2 cannot be represented in Objective-C
我错过了什么吗?有什么解决方法吗?
最佳答案
你必须像 Objective-C 一样思考。当翻译成 Objective-C 时,这个声明可能意味着什么?特别是,[CacheManagerType]
是什么意思?它必须是一个数组,即 NSArray,包含 CacheManagerType 对象。
但这是不可能的。 CacheManagerType 是一个枚举。桥接到 Objective-C 的 Int 枚举被转换为 Objective-C 枚举。为了让你的方法声明在 Objective-C 中工作,Objective-C 需要能够理解枚举数组的概念——但它不能。在 Objective-C 中,枚举不是对象,但 NSArray 只能容纳对象。
关于objective-c - 桥接 Swift 4 枚举到 Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46605884/