简体   繁体   English

如何指定组合类型 CustomStringConvertible 和 RawRepresentable 的函数参数的类型?

[英]How to specify the type of a function parameter with combined type CustomStringConvertible and RawRepresentable?

I want a generic function that can instantiate object of a few different enum types I have by supplying the enum type and the Int raw-value.我想要一个通用函数,它可以通过提供枚举类型和Int原始值来实例化我拥有的几种不同enum类型的对象。 These enum s are also CustomStringConvertible .这些enum也是CustomStringConvertible

I tried this:我试过这个:

func myFunc(type: CustomStringConvertible.Type & RawRepresentable.Type, rawValue: Int)

which results in 3 errors:这导致 3 个错误:

  • Non-protocol, non-class type 'CustomStringConvertible.Type' cannot be used within a protocol-constrained type非协议、非类类型“CustomStringConvertible.Type”不能在协议约束类型中使用
  • Non-protocol, non-class type 'RawRepresentable.Type' cannot be used within a protocol-constrained type非协议、非类类型“RawRepresentable.Type”不能在协议约束类型中使用
  • Protocol 'RawRepresentable' can only be used as a generic constraint because it has Self or associated type requirements协议 'RawRepresentable' 只能用作通用约束,因为它具有 Self 或关联的类型要求

Forgetting about 'CustomStringConvertible` for now, I also tried:暂时忘记“CustomStringConvertible”,我也尝试过:

private func myFunc<T: RawRepresentable>(rawValue: Int, skipList: [T]) {
    let thing = T.init(rawValue: rawValue)
}

But, although code completion suggests it, leads to an error about the T.init(rawValue:) :但是,尽管代码完成建议它,导致关于T.init(rawValue:)的错误:

  • Cannot invoke 'init' with an argument list of type '(rawValue: Int)'无法使用类型为“(rawValue: Int)”的参数列表调用“init”

How can I form a working generic function like this?我怎样才能形成这样的工作通用函数?

The issue is that T.RawValue can be something else than Int with your current type constraints.问题是T.RawValue可以是Int以外的其他东西,并且具有您当前的类型约束。 You need to specify that T.RawValue == Int in order to pass your rawValue: Int input parameter to init(rawValue:) .您需要指定T.RawValue == Int以便将您的rawValue: Int输入参数传递给init(rawValue:)

func myFunc<T: RawRepresentable & CustomStringConvertible>(rawValue: Int, skipList: [T]) where T.RawValue == Int {
    let thing = T.init(rawValue: rawValue)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Swift 中打开 RawRepresentable 枚举类型 - How to switch on a RawRepresentable enum type in Swift 如何将泛型指定为变量? - How to specify generic type as variable? 如何在处理程序中指定返回类型 - How to specify return type in handler 如何将 function 中泛型类型的参数约束为兄弟参数 - How To constraint parameter of generic type in function, to sibling parameter 在实现ErrorProtocol时,不断收到错误1)使用未声明的类型ErrorProtocol 2)类型不符合协议RawRepresentable - when implementing ErrorProtocol, keep getting error 1) use of undeclared type ErrorProtocol 2) type does not conform to protocol RawRepresentable iOS Swift4如何协调T.Type和type(of :)传递动态类类型作为函数参数? - iOS Swift4 how to reconcile T.Type and type(of:) to pass dynamic class type as function parameter? 函数调用参数类型与URL类型不匹配 - Function call parameter type mismatch with URL type 无法将Enum类型作为函数的参数类型 - can not pass Enum type as parameter type of function 使用类型为Dictionary的参数声明的函数显示另一种参数类型 - Function declared with a parameter of type Dictionary shows another parameter type 如何在函数中指定参数可以在Swift中为nil - How to specify the parameter in a function can be nil in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM