切换案例语句 - 通用关联类型

时间:2018-05-26 15:58:16

标签: ios swift generics enums

我正在尝试将泛型类型与switch case语句相关联,但我收到编译时错误。

if (mInterstitialAd.isReady)
{
    DispatchQueue.main.async
    {
        mInterstitialAd.present(fromRootViewController: self)
    }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

enum本身必须声明为通用,而不是它的情况,你不能在case声明中使用where子句,你需要在关联值上指定泛型类型约束。

enum TextEditEvent<T>{
    case editingBegin(UITextField)
    case editingEnd(UITextField, UITextField?)
    case textChanged(String?, UILabel?, T:Object, Updateable, String)
}

或者,如果您希望T在整个enum中包含这些类型约束,不仅仅针对textChanged案例,您可以像这样声明enum:< / p>

enum TextEditEvent<T: Object, Updateable>{
    case editingBegin(UITextField)
    case editingEnd(UITextField, UITextField?)
    case textChanged(String?, UILabel?, T, String)
}
相关问题