我正在尝试编写一个函数,该函数接受由RawRepresentable生成的任何CustomStringConvertible值。我试着写了这个:
enum MyEnum: String {
case a = "someString"
}
func myFunction<R: RawRepresentable>(val: R) where R.RawValue == CustomStringConvertible {
print(val.rawValue.description)
}
myFunction(val: MyEnum.a)但是,我得到了以下错误:
Global function 'myFunction(val:)' requires the types 'String' and 'CustomStringConvertible' be equivalent这很奇怪,因为String确实符合CustomStringConvertible。
但是,将RawValue与just String相结合是可行的,但是,我想将其与其他CustomStringConvertible一起使用。
为什么这不编译,我有办法做到这一点吗?
发布于 2019-10-04 13:24:27
你应该说它符合协议
where R.RawValue: CustomStringConvertible 现在它也适用于其他类型。
enum MyEnum2: Int {
case one = 1
}
myFunction(val: MyEnum2.one)https://stackoverflow.com/questions/58237226
复制相似问题