我正在尝试在@objc 协议(protocol)中创建一个可选函数,文档类型也是@objc 枚举。但是我收到了这个错误:
Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C
我的源代码:
@objc enum DocumentType: Int {
case pdf
case png
}
@objc protocol ImageDocumentEditorProtocol: class {
@objc optional func imageDocumentEditorDidCancel(documentType: DocumentType?)
}
我该如何解决这个问题? 谢谢
最佳答案
Objective-C 没有可选的枚举。枚举必须是非可选的。类可以是可选的,不是枚举:(
一种解决方法是添加一个案例:
@objc enum DocumentType: Int {
case pdf
case png
case none
}
并改用非可选类型DocumentType
。
当然,这使得非可选的 DocumentType
不可表示。要表示可选和非可选的 DocumentType
,您需要两种类型:
@objc enum DocumentType: Int {
case pdf
case png
func asOptionalDocumentType() -> OptionalDocumentType {
switch self {
case .pdf: return .pdf
case .png: return .png
}
}
}
extension Optional where Wrapped == DocumentType {
func asOptionalDocumentType() -> OptionalDocumentType {
self?.asOptionalDocumentType() ?? .none
}
}
@objc enum OptionalDocumentType: Int, ExpressibleByNilLiteral {
case pdf
case png
case none
func asDocumentType() -> DocumentType? {
switch self {
case .pdf: return .pdf
case .png: return .png
case .none: return nil
}
}
init(nilLiteral: ()) {
self = .none
}
}
我添加了转换方法以方便它们之间的转换,但技术上不需要它们。
关于ios - 将可选的@objc 枚举类型传递给@objc 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62788178/