【发布时间】:2019-04-09 18:37:06
【问题描述】:
有没有办法在swift的函数指针中设置协议的数据类型?
这是我的协议ICRUDOperation
public protocol ICRUDOperation {
associatedtype T
func insert(data:T)
func update(data:T)
func get(data:T) -> [T]
func getList(data: BaseModel) -> [T]
func getPage(data: BaseModel) -> [T]
func delete(data: T)
}
我尝试使用的:
func delegate1<W>(sqlite: W, service: W, data: W.T) where W: ICRUDOperation {
sqlite.insert(data: data)
}
var decision = [String : [String:((ICRUDOperation, ICRUDOperation, T) ->())?]]()
func fillDecision() {
decision["Person"]?["1"] = Delegate1
}
我在决定中遇到此错误
Protocol 'ICRUDOperation' can only be used as a generic constraint because it has Self or associated type requirements
fillDecision() 出错:
Cannot assign value of type '(_, _, _.T) -> ()' to type '((ICRUDOperation, ICRUDOperation, _) -> ())??'
【问题讨论】:
-
我将函数名和大多数变量名中的前导大写字母改为小写,这是 Swift 标准,以提高可读性