[英]Switch case statement - Generic associated type
我正在嘗試將通用類型與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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.