将可选的@objc枚举类型传递给@objc协议

时间:2020-07-08 05:33:54

标签: ios swift protocols

我正在尝试在@objc协议中创建一个可选函数,documentType也是@objc枚举。但我收到此错误:

方法不能标记为@objc,因为参数的类型不能 用Objective-C表示

我的源代码:

a: [15, 14, 39, 23, 14, 14, 8]
upb: 39
gcc: [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
z: 3
max_val: 14
ub_array(a): 8..39
----------
==========

如何解决此问题? 谢谢

3 个答案:

答案 0 :(得分:1)

问题是?

在Objective-C中,您不能表示基本类型的Optional。

将其设为非可选,或找到其他方式。

答案 1 :(得分:1)

只需删除DocumentType上的可选内容,即可使用以下功能:

@objc optional func imageDocumentEditorDidCancel(documentType: DocumentType)

如果要在此处表示nil值,可以在枚举中为其添加另一种情况,如下所示:

@objc enum DocumentType: Int {
    case pdf
    case png
    case none
}

答案 2 :(得分:1)

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 } } ,则需要两种类型:

targetCenter = Vector3f(x, y, z)

viewMatrix
    .translate(targetCenter)
    .rotateX(rotation)
    .translate(targetCenter.mul(-1.0f))

我添加了转换方法,以使它们之间的转换变得容易,但是从技术上来说并不是必需的。