这是我的代码
protocol sharedFunction {
func transformDictionary<Element>(postDictionary:[Element:Element], key: Element) -> Element
}
class newUser: sharedFunction {
var email:String?
var username:String?
var uid: String?
func transformDictionary(postDictionary: [String : Any], key: String) -> newUser {
}
}
但我不断收到此错误“Type newUser”不符合协议(protocol)““sharedFunction”
最佳答案
错误的发生仅仅是因为您的类型不符合消息明确指出的协议(protocol)。我会像这样重写你的代码:
protocol SharedFunction {
associatedtype Element: Hashable
func transformDictionary(postDictionary: [Element:Element], key: Element) -> Element
}
class NewUser: SharedFunction {
typealias Element = Int
var email: String?
var username: String?
var uid: String?
func transformDictionary(postDictionary: [Element : Element], key: Element) -> Element {
return 1
}
}
我不知道这段代码的上下文是什么,所以我不知道我的代码对你是否有意义,但我想你明白了。
关于ios - 如何在协议(protocol)中声明泛型函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44836788/