ios - Switch case 语句 - 通用关联类型
问题描述
我正在尝试将泛型类型与 switch case 语句相关联,但出现编译时错误。
enum TextEditEvent{
case editingBegin(UITextField)
case editingEnd(UITextField, UITextField?)
case textChanged<T>(String?, UILabel?, T, String) where T:Object, T:Updatable
}
任何帮助将非常感激。
解决方案
它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
这样声明:
enum TextEditEvent<T: Object, Updateable>{
case editingBegin(UITextField)
case editingEnd(UITextField, UITextField?)
case textChanged(String?, UILabel?, T, String)
}
推荐阅读
- javascript - 由于某种原因,掷骰子游戏变量未定义
- jenkins - 将“本地”Jenkins 的最新构建工件发布到 Azure DevOps Release Pipeline?
- c# - 如何查看另一个字符串列表中是否包含(不等于)字符串列表
- python - 对列中的每一行应用数据清洗日期函数
- python - 当我使用 MNIST 数据集时,它有一个错误
- little-man-computer - LMC:包括从十进制到基数 9 的基本转换
- kendo-ui - 剑道网格编辑弹出 mvvm 绑定似乎失败
- r - 如何为绘图上的每个因素添加回归线
- php - 如何修改不带附件发送消息的代码?
- php - Asgardcms - 未定义变量:创建新博客文章时的媒体